When we write J2ME programs, we try to manipulate strings, and there are many string operations in the J2SE that we don't have in J2ME, which we have to write ourselves manually. I've sorted out my usual string manipulation methods:
1.URLEncode (string URL), the special character of the string is encoded, this method is very ordinary, our httpconnection request must use this method.
2.split (string s, int chr), this method is used to split the string, to return an array of strings, when some data in a specific character interval, we take out the data to try to use this method.
3.replaceAll (string from, string to, string source), this method is used to replace substrings in strings, such as: ReplaceAll ("AAA", "", "Aaabbbcccaaa") The string " All "AAA" in AAABBBCCC is replaced with an empty string, and the result is "BBBCCC".
4.StringFilter (String str), this method is used to filter certain characters.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
Package test;
Import Java.util.Vector;
/**
*
* @author Administrator
*/
public class Stringutil {
/**
* UrlEncode to encode special characters
* @param URL
* @return
*/
public static string UrlEncode (string url) {
StringBuffer buf = new StringBuffer ();
for (int i = 0; i < url.length (); i++) {
Switch (Url.charat (i)) {
Case ':
Buf.append ("%20");
Break
Case ' + ':
Buf.append ("%2b");
Break
Case ' \ ':
Buf.append ("%27");
Break
Case '/':
Buf.append ("%2f");
Break
Case '. ':
Buf.append ("%2e");
Break
Case ' < ':
Buf.append ("%3c");
Break
Case ' > ':
Buf.append ("%3e");
Break
Case ' # ':
Buf.append ("%23");
Break
Case '% ':
Buf.append ("%25");
Break
Case ' & ':
Buf.append ("%26");
Break
Case ' {':
Buf.append ("%7b");
Break
Case '} ':
Buf.append ("%7d");
Break
Case ' \ ':
Buf.append ("%5c");
Break
Case ' ^ ':
Buf.append ("%5e");
Break
Case ' ~ ':
Buf.append ("%73");
Break
Case ' [':
Buf.append ("%5b");
Break
Case '] ':
Buf.append ("%5d");
Break
Default
Buf.append (Url.charat (i));
Break
}
}
return buf.tostring ();
}
/**
* Split a string into a string array with a char as the split point
* @param s
* @param chr
* @return
*/
public static string[] Split (String s, int chr) {
Vector res = new vector ();
int Curr;
int prev = 0;
while ((Curr = S.indexof (CHR, prev)) >= 0) {
Res.addelement (s.substring (prev, curr));
Prev = Curr + 1;
}
Res.addelement (s.substring (prev));
string[] splitted = new string[res.size ()];
Res.copyinto (splitted);
return splitted;
}
/**
* String substitution
* @param from
* substring to be replaced in string
* @param to
* Replace strings of substring
* @param source
* The string being manipulated
* @return
*/
public static string ReplaceAll (string from, string to, string source) {
if (Source = null | | from = = NULL | | to = = NULL) {
return null;
}
StringBuffer bf = new StringBuffer ();
int index =-1;
while (index = Source.indexof (from))!=-1) {
Bf.append (source.substring (0, index) + to);
Source = source.substring (index + from.length ());
index =-1;
}
Bf.append (source);
return bf.tostring ();
}
/**
* String Filter
* Filter out labels such as * For example str = * @param str
* @return
*/
public static string Stringfilter (String str) {
String now = str;
int start = 0;
int end = 0;
for (int i = 0; i < now.length (); i++) {
' \u003c ', ' \u003e ' = <>
if (Now.charat (i) = = ' \u003c ') {
start = i;
}
if (Now.charat (i) = = ' \u003e ') {
end = i;
}
If (Start!=-1 && End!=-1) {
String sub = now.substring (start, end+1);
str = Stringutil.replaceall (Sub, "", str);
start = 0;
end = 0;
}
}
return str;
}
}