我想知道如何在黑莓中拆分字符串 str.split() 函数似乎不可用
问问题
1516 次
4 回答
2
看看这个,最简单的
public static String[] split(String str, char c) {
int index = str.indexOf(c);
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c)
count++;
}
String[] words = new String[++count];
int counter = 0;
while (index >= 0) {
words[counter] = str.substring(0, index);
str = str.substring(index + 1,str.length()).trim();
counter++;
index = str.indexOf(c);
}
words[counter] = str;
return words;
}
于 2012-06-06T05:53:38.573 回答
1
是的,你是对的,黑莓 api 中没有提供 split() 函数。我必须使用它,所以我通过这种方式做到了。可能对你也有帮助。
public static String[] split(String original, String separator) {
Vector nodes = new Vector();
int index = original.indexOf(separator);
while (index >= 0) {
nodes.addElement(original.substring(0, index));
original = original.substring(index + separator.length());
index = original.indexOf(separator);
}
nodes.addElement(original);
String[] result = new String[nodes.size()];
if (nodes.size() > 0) {
for (int loop = 0; loop < nodes.size(); loop++) {
result[loop] = (String) nodes.elementAt(loop);
System.out.println("Value inside result is ........ "+ result[loop]);
}
}
return result;
}
于 2012-06-06T05:32:12.000 回答
0
@YAK:试试这个..
public void split(String Word,Char delimiter)
String[] arr = new String[5];
String text = "pen, pencil,book,123,note";
text=text+delimiter;
int n = 0;
for (int i = 0; i < text.length(); i++)
{
int s = text.indexOf(delimiter);
add(new RichTextField(Integer.toString(s)));
if(s==0)
{
arr[n]="null";
if(text.length()>1)
{
text = text.substring(1,text.length());
i = 0;
n++;
}
}
else
{
arr[n] = text.substring(0, s);
s = s + 1;
text = text.substring(s,text.length());
// add(new RichTextField("txt"+text));
i = 0;
n++;
}
}
for (int i = 0; i < arr.length; i++)
{
if (arr[i] != null)
{
add(new RichTextField("value: "+arr[i]));
}
}
于 2012-06-06T13:32:48.777 回答
0
这是首选链接
如果上面的链接不起作用,那么请继续关注
public String[] split(String strString, String strDelimiter)
{
int iOccurrences = 0;
int iIndexOfInnerString = 0;
int iIndexOfDelimiter = 0;
int iCounter = 0;
// Check for null input strings.
if (strString == null)
{
throw new NullPointerException("Input string cannot be null.");
}
// Check for null or empty delimiter
// strings.
if (strDelimiter.length() <= 0 || strDelimiter == null)
{
throw new NullPointerException("Delimeter cannot be null or empty.");
}
// If strString begins with delimiter
// then remove it in
// order
// to comply with the desired format.
if (strString.startsWith(strDelimiter))
{
strString = strString.substring(strDelimiter.length());
}
// If strString does not end with the
// delimiter then add it
// to the string in order to comply with
// the desired format.
if (!strString.endsWith(strDelimiter))
{
strString += strDelimiter;
}
// Count occurrences of the delimiter in
// the string.
// Occurrences should be the same amount
// of inner strings.
while((iIndexOfDelimiter= strString.indexOf(strDelimiter,iIndexOfInnerString))!=-1)
{
iOccurrences += 1;
iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
}
// Declare the array with the correct
// size.
String[] strArray = new String[iOccurrences];
// Reset the indices.
iIndexOfInnerString = 0;
iIndexOfDelimiter = 0;
// Walk across the string again and this
// time add the
// strings to the array.
while((iIndexOfDelimiter= strString.indexOf(strDelimiter,iIndexOfInnerString))!=-1)
{
// Add string to
// array.
strArray[iCounter] = strString.substring(iIndexOfInnerString, iIndexOfDelimiter);
// Increment the
// index to the next
// character after
// the next
// delimiter.
iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
// Inc the counter.
iCounter += 1;
}
return strArray;
}
于 2012-06-06T08:13:08.190 回答