我有一個字符串tajmahal.txt sarjan.pdf noorjahan.exe
。我想將這個字符串存儲在一個字符串數組中,例如ar[0] = tajmahal.txt
,ar[1] = sarjan.pdf
和ar[2] = noorjahan.exe
。如何在java中不使用任何構建函數(因爲我使用不支持許多j2se函數的j2me)來實現它。任何幫助都會很棒。提前致謝。將部分字符串存儲在字符串數組中
0
A
回答
0
由於String.split不可用。你可以看到從這個answer
public static String[] Split(String splitStr, String delimiter) {
StringBuffer token = new StringBuffer();
Vector tokens = new Vector();
// split
char[] chars = splitStr.toCharArray();
for (int i=0; i < chars.length; i++) {
if (delimiter.indexOf(chars[i]) != -1) {
// we bumbed into a delimiter
if (token.length() > 0) {
tokens.addElement(token.toString());
token.setLength(0);
}
} else {
token.append(chars[i]);
}
}
// don't forget the "tail"...
if (token.length() > 0) {
tokens.addElement(token.toString());
}
// convert the vector into an array
String[] splitArray = new String[tokens.size()];
for (int i=0; i < splitArray.length; i++) {
splitArray[i] = (String)tokens.elementAt(i);
}
return splitArray;
}
0
String str="tajmahal.txt sarjan.pdf noorjahan.exe";
StringTokenizer st=new StringTokenizer(str," ");
String[] arr=new String[st.countTokens()];
int i=0;
while (st.hasMoreElements()){
arr[i]=st.nextToken();
i++;
}
+0
This is working –
0
這要看Java ME的配置/情景模式設置您使用分割的實現。
當涉及到CLDC/MIDP,其中沒有集合,StringTokenizer,split()等。
- 先計算這些空間('')。
- 準備一個String [count + 1]。
- 在用StringBuffer緩衝字符的同時分割每個標記。
static String[] split(final String string) {
// count spaces
int spaces = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == 0x20) {
spaces++;
}
}
// prepare the array and buffer
final String[] split = new String[spaces + 1];
final StringBuffer buffer = new StringBuffer();
int index = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == 0x20) {
split[index++] = buffer.toString();
buffer.delete(0, buffer.length());
continue;
}
buffer.append(string.charAt(i));
}
return split;
}
0
有在J2ME中的StringTokenizer的實現。 檢出this example以幫助您完成任務。
StringTokenizer token;
token = new StringTokenizer(str);
int i=0;
while(token.hasMoreElements()){
ar[i++]= tok.nextToken();
}
相關問題
- 1. 在數組中存儲字符串並拆分字符串
- 2. 將字符串的地址存儲在字符串數組中
- 3. 將字符串存儲到數組中
- 4. 將字符串存儲到數組中?
- 5. Java:將字符串的特定部分存儲到數組中
- 6. 在字符串中存儲字符串
- 7. 在二維字符串數組中存儲很多字符串
- 8. 在SharedPreferences中存儲字符串數組
- 9. 將字符串拆分爲兩部分並將其存儲在數組中
- 10. MATLAB將字符串存儲爲數組
- 11. 在字符串內存儲字符串?
- 12. 如何將字符串生成的子串存儲到字符串數組中?
- 13. 將字符串分割成單個字符字符串數組
- 14. 分解字符串並將其存儲在數組中
- 15. 字符串數組中的部分字符串
- 16. 比較字符串數組中的一部分字符串
- 17. 查找字符串數組中的字符串的一部分
- 18. 將字符串存儲在不帶空字符的字符數組中
- 19. mySQL只存儲部分字符串
- 20. 如何將字符串中存儲的數據轉換爲字符串數組
- 21. 存儲字符串數組在appSettings?
- 22. 在字符串數組存儲頭VBA
- 23. 如何將字符串值存儲在Java數組中的字符數組?
- 24. C#拆分字符串 - 將字符串拆分爲數組
- 25. 搜索子字符串並在Perl中存儲字符串的另一部分
- 26. JavaScript將部分字符串替換爲部分字符串?
- 27. 在一個鋸齒形數組中存儲字符串字符
- 28. 在二維數組中存儲字符串和字符(C)
- 29. java在字符數組中存儲字符串
- 30. 將字符數組列存儲到字符串進行比較
複製的[如何在J2ME分割字符串?](http://stackoverflow.com/questions/200746/how-do-i-split-strings-in-j2me) – angelsl
你想創建您自己的自定義功能?因爲您可以在Java中使用Split功能來執行此操作。 編輯:沒關係,我剛纔看到這個功能在J2ME中不可用。 – MrByte
查看這些答案 –