2012-06-06 57 views
0

拆分字符串我想知道如何在黑莓 分裂的字符串str.split()函數似乎是不可用是的,你說得對,不提供如何在黑莓

+0

獲得字符串的長度,並使用子方法拆分 – Signare

+0

str.substring(0,5);這會將字符串從開始分割爲5個。 – Signare

+0

@YAK,你檢查了答案嗎?我認爲他們中的任何人都能解決你的問題。爲什麼這個問題沒有被標記爲解決? – Rupak

回答

1

分割()函數在黑莓api中。我必須使用它,所以我通過這種方式做到了。可能會對你有所幫助。

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; 
} 
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; 
    } 
+0

一個小問題:在'while'循環中應該是'str.substring(index + 1,str.length())。trim();'而不是'str.substring(index,str.length()) .trim();'。 – Pablo

+0

@Pablo。謝謝你指出我 –

0

這是優選的鏈接

http://supportforums.blackberry.com/t5/Java-Development/String-Manipulation-split-replace-replaceAll/ta-p/620038

如果上面的鏈接無法再工作去throgh以下

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; 
    } 
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])); 

     } 

    }