2015-12-01 25 views
0
非字母字符

從此我的目標是轉換「<,大膽,>」爲「<大膽>」(沒有「<」 &「B」之間的空間)使用的BreakIterator跳過具有的BreakIterator

說字符串「這是一個測試。」是我的輸入

public static List<String> getWords(String text) { 
    List<String> words = new ArrayList<String>(); 
    BreakIterator breakIterator = BreakIterator.getWordInstance(); 
    breakIterator.setText(text); 
    int lastIndex = breakIterator.first(); 
    while (BreakIterator.DONE != lastIndex) { 
     int firstIndex = lastIndex; 
     lastIndex = breakIterator.next(); 
     if (lastIndex != BreakIterator.DONE) { 
      String t = text.substring(firstIndex, lastIndex); 
      words.add(t); 
     } 
    } 
    return words; 
} 

getWords(String)返回<,粗體,>,這是,一個,測試。

我已經試過:

  String t = text.substring(firstIndex, lastIndex); 
      if (t != "<" || t != ">" || t != "/" || t != ">") System.out.println("Char Not Skipped " + t); else System.out.println("Char Skipped" + t); 
      //if (text.charAt (firstIndex - 1) == '<') t = "<" + t; 
      //if (text.charAt (lastIndex + 1) == '>') t += ">"; 
      //if (text.charAt (lastIndex + 1) == '/' && text.charAt (lastIndex + 2) == '>') t += "/>"; 
      //System.out.println(t); 
      words.add(t); 

所有返回是字符不會被跳過。

+0

我從未使用過。但是'replaceAll(regex)'在這種情況下是更好的選擇。 –

回答

1

我不知道我是否以正確的方式得到了您的問題。

如果你想DELET字符串中的所有,,你可以很容易地做到:

String s = "<,Bold,>, ,This, ,is, ,a, ,test"; 
    String newString = s.replace(",", ""); 
    System.out.println(newString); 

輸出會看起來像:

這是一個測試

如果你只是想刪除<,,>你可以使用:

String s = "<,Bold,>, ,This, ,is, ,a, ,test"; 
    String newString = (s.replace("<,", "<")).replace(",>", ">"); 
    System.out.println(newString); 

的是輸出將爲

<Bold>,這,是,一,測試

+0

使用我得到:'這是一個測試' – nullpointer

+0

@nullpointer是不是他想要的? –