📄 textutil.java
字号:
ArrayList list )
{
char[] valueChars = value.toCharArray();
int startPos = 0;
int lastSpacePos = -1;
int lastSpacePosLength = 0;
int currentLineWidth = 0;
for (int i = 0; i < valueChars.length; i++) {
char c = valueChars[i];
currentLineWidth += font.charWidth( c );
if (c == '\n') {
list.add( new String( valueChars, startPos, i - startPos ) );
lastSpacePos = -1;
startPos = i+1;
currentLineWidth = 0;
firstLineWidth = lineWidth;
i = startPos;
} else if (currentLineWidth >= firstLineWidth && i > 0) {
if ( lastSpacePos == -1 ) {
i--;
//System.out.println("value=" + value + ", i=" + i + ", startPos=" + startPos);
list.add( new String( valueChars, startPos, i - startPos ) );
startPos = i;
currentLineWidth = 0;
} else {
currentLineWidth -= lastSpacePosLength;
list.add( new String( valueChars, startPos, lastSpacePos - startPos ) );
startPos = lastSpacePos + 1;
lastSpacePos = -1;
}
firstLineWidth = lineWidth;
} else if (c == ' ' || c == '\t') {
lastSpacePos = i;
lastSpacePosLength = currentLineWidth;
}
}
// add tail:
list.add( new String( valueChars, startPos, valueChars.length - startPos ) );
/*
try {
int widthPerChar = (completeWidth * 100) / valueChars.length;
int startIndex = 0;
while (true) {
int index = (firstLineWidth * 100) / widthPerChar;
int i = index + startIndex;
if (i >= valueChars.length) {
// reached the end of the given string:
list.add( new String( valueChars, startIndex, valueChars.length - startIndex ) );
break;
}
// find the last word gap:
while (valueChars[i] != ' ') {
i--;
if (i < startIndex ) {
// unable to find a gap:
i = index + startIndex;
break;
}
}
//TODO rob maybe check whether the found string really fits into the line
list.add( new String( valueChars, startIndex, i - startIndex ) );
if (valueChars[i] == ' ' || valueChars[i] == '\t') {
startIndex = i + 1;
} else {
startIndex = i;
}
firstLineWidth = lineWidth;
}
} catch (ArithmeticException e) {
System.out.println("unable to split: " + e);
e.printStackTrace();
System.out.println("complete width=" + completeWidth + " number of chars=" + value.length());
}
*/
}
// Unreserved punctuation mark/symbols
/**
* Converts Hex digit to a UTF-8 "Hex" character
*
* @param digitValue digit to convert to Hex
* @return the converted Hex digit
*/
private static char toHexChar(int digitValue) {
if (digitValue < 10)
// Convert value 0-9 to char 0-9 hex char
return (char)('0' + digitValue);
else
// Convert value 10-15 to A-F hex char
return (char)('A' + (digitValue - 10));
}
/**
* Encodes a URL for - This method assumes UTF-8
*
* @param url URL to encode
* @return the encoded URL
*/
public static String encodeUrl(String url) {
StringBuffer encodedUrl = new StringBuffer(); // Encoded URL
int len = url.length();
// Encode each URL character
for (int i = 0; i < len; i++) {
char c = url.charAt(i); // Get next character
if ((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z')) {
// Alphanumeric characters require no encoding, append as is
encodedUrl.append(c);
} else {
int imark = UNRESERVED.indexOf(c);
if (imark >=0) {
// Unreserved punctuation marks and symbols require
// no encoding, append as is
encodedUrl.append(c);
} else {
// Encode all other characters to Hex, using the format "%XX",
// where XX are the hex digits
encodedUrl.append('%'); // Add % character
// Encode the character's high-order nibble to Hex
encodedUrl.append(toHexChar((c & 0xF0) >> 4));
// Encode the character's low-order nibble to Hex
encodedUrl.append(toHexChar (c & 0x0F));
}
}
}
return encodedUrl.toString(); // Return encoded URL
}
/**
* Replaces the all matches within a String.
*
* @param input the input string
* @param search the string that should be replaced
* @param replacement the replacement
* @return the input string where the search string has been replaced
* @throws NullPointerException when one of the specified strings is null
*/
public static String replace( String input, String search, String replacement ) {
int pos = input.indexOf( search );
if (pos != -1) {
StringBuffer buffer = new StringBuffer();
int lastPos = 0;
do {
buffer.append( input.substring( lastPos, pos ) )
.append( replacement );
lastPos = pos + search.length();
pos = input.indexOf( search, lastPos );
} while (pos != -1);
buffer.append( input.substring( lastPos ));
input = buffer.toString();
}
return input;
}
/**
* Replaces the first match in a String.
*
* @param input the input string
* @param search the string that should be replaced
* @param replacement the replacement
* @return the input string where the first match of the search string has been replaced
* @throws NullPointerException when one of the specified strings is null
*/
public static String replaceFirst( String input, String search, String replacement ) {
int pos = input.indexOf( search );
if (pos != -1) {
input = input.substring(0, pos) + replacement + input.substring( pos + search.length() );
}
return input;
}
/**
* Replaces the last match in a String.
*
* @param input the input string
* @param search the string that should be replaced
* @param replacement the replacement
* @return the input string where the last match of the search string has been replaced
* @throws NullPointerException when one of the specified strings is null
*/
public static String replaceLast( String input, String search, String replacement ) {
int pos = input.indexOf( search );
if (pos != -1) {
int lastPos = pos;
while (true) {
pos = input.indexOf( search, lastPos + 1 );
if (pos == -1) {
break;
} else {
lastPos = pos;
}
}
input = input.substring(0, lastPos) + replacement + input.substring( lastPos + search.length() );
}
return input;
}
/**
* Retrieves the last index of the given match in the specified text.
*
* @param text the text in which the match is given
* @param match the match within the text
* @return the last index of the match or -1 when the match is not found in the given text
* @throws NullPointerException when text or match is null
*/
public static int lastIndexOf(String text, String match) {
int lastIndex = -1;
int index = text.indexOf( match );
while (index != -1) {
lastIndex = index;
index = text.indexOf( match, lastIndex + 1 );
}
return lastIndex;
}
/**
* Compares two strings in a case-insensitive way. Both strings are lower cased and
* then compared. If both are equal this method returns <code>true</code>,
* <code>false</code> otherwise.
*
* @param str1 the string to compare
* @param str2 the string to compare to
*
* @return <code>true</code> if both strings are equals except case,
* <code>false</code>
*
* @throws NullPointerException if <code>str1</code> is <code>null</code>
*
* @see String#equals(Object)
* @see String#equalsIgnoreCase(String)
*/
public static boolean equalsIgnoreCase(String str1, String str2)
{
//#if polish.cldc1.1
//# return str1.equalsIgnoreCase(str2);
//#else
if (str1 != null && str2 == null)
{
return false;
}
return str1.toLowerCase().equals(str2.toLowerCase());
//#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -