2014-03-14 36 views
0

我想要以長字符串的一個字母開頭的所有單詞。你會怎麼做這是Java?我不想循環閱讀每封信或效率低下的東西。Java查找用字母開頭的所有單詞

編輯:我也不能使用任何內置的數據結構(當然數組除外) - 它爲一個CS類。但是我可以創建自己的數據結構(我已經創建了幾個數據結構)。

+0

如果它的任何安慰,看着字符串中的每個字符無法避免(因爲你不知道*一先驗*空間在哪裏)。 – NPE

+1

讓我們看看你的解決方案/嘗試(代碼),並擔心之後的效率。另外,定義「長」。 – reto

+0

我認爲這可能幫助: 1.分割 http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java 2.然後檢查是否有 「信」 http://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex – Bjego

回答

0

你可以建立一個HashMap -

HashMap<String,String> map = new HashMap<String,String>(); 

例子 -

ant, bat, art, cat 

Hashmap 
a -> ant,art 
b -> bat 
c -> cat 

查找以 「A」 開頭的所有單詞,只是做

map.get("a") 
+2

撇開數據結構的細節,如果您事先知道起始字母,爲什麼還需要存儲單詞? – NPE

+0

在運行時取詞? –

+0

當然我們不需要存儲字符串,取決於輸入,如果它是一個數組,我們可以將索引存儲在一個數組中作爲hashmap中的值。 –

0
Scanner scan = new Scanner(text); // text being the string you are looking in 
char test = 'x'; //whatever letter you are looking for 
while(scan.hasNext()){ 
    String wordFound = scan.next(); 
    if(wordFound.charAt(0)==test){ 
     //do something with the wordFound 
    } 
} 

這將做你正在尋找的,我nside if語句做你想要的單詞

0

你可以使用split()方法。這裏是一個例子:

String string = "your string"; 
String[] parts = string.split(" C"); 

for(int i=0; i<parts.length; i++) { 
    String[] word = parts[i].split(" "); 

    if(i > 0) { 
      // ignore the rest words because don't starting with C 
     System.out.println("C" + word[0]); 
    } 
else { // Check 1st excplicitly 
      for(int j=0; j<word.length; j++) { 

     if (word[j].startsWith("c") || word[j].startsWith("C")) 
       System.out.println(word[j]); 
      } 
     } 

    } 

其中「C」是你的信。然後循環陣列。對於零件[0],您必須檢查它是否以「C」開頭。從i = 1開始循環是我的錯誤。正確的是從0

+1

,看起來像它的權利。謝謝 –

+1

這要麼太微妙,要麼完全錯誤。 (我懷疑後者。)如果是前者,請展開。 – NPE

+0

等它是錯的。 –

2

你可以嘗試從您的字符串獲得一個數組集合,然後通過它迭代:

String s = "my very long string to test"; 

for(String st : s.split(" ")){ 
    if(st.startsWith("t")){ 
     System.out.println(st); 
    } 
} 
0

你可以得到字符串的第一個字母,並與API方法來檢查,如果它是信還是不信。

String input = "jkk ds 32"; 
String[] array = input.split(" "); 
for (String word : array) { 
    char[] arr = word.toCharArray(); 
    char c = arr[0]; 
    if (Character.isLetter(c)) { 
     System.out.println(word + "\t isLetter"); 
    } else { 
     System.out.println(word + "\t not Letter"); 
    } 
} 

以下是一些示例輸出:

jkk isLetter 
ds isLetter 
32 not Letter 
+0

@reto好吧..我會做一個編輯 – guptakvgaurav

0

正則表達式的方法:

public static void main(String[] args) { 
    String text = "my very long string to test"; 
    Matcher m = Pattern.compile("(^|\\W)(\\w*)").matcher(text); 
    while (m.find()) { 
     System.out.println("Found: "+m.group(2)); 
    } 
} 
2

你需要清楚一些事情。什麼是「單詞」?你只想找到以字母開頭的「單詞」,所以我假設單詞也可以有其他角色。但是什麼字符是允許的?什麼定義了這樣一個詞的開始?空白,任何非字母,任何非字母/非數字,...?

如:

String TestInput = "test séntènce îwhere I'm want,to üfind 1words starting $with le11ers."; 
String regex = "(?<=^|\\s)\\pL\\w*"; 

Pattern p = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS); 

Matcher matcher = p.matcher(TestInput); 
while (matcher.find()) { 
    System.out.println(matcher.group()); 
} 

正則表達式(?<=^|\s)\pL\w*會發現這樣的序列以字母開頭(\pL是字母A Unicode property),其次是因爲修飾符的0個或多個"word" characters(Unicode字母和數字, Pattern.UNICODE_CHARACTER_CLASS)。
lookbehind assertion(?<=^|\s)確保在序列之前有字符串或空白的開始。

所以我的代碼將打印:

test 
séntènce ==> contains non ASCII letters 
îwhere ==> starts with a non ASCII letter 
I  ==> 'm is missing, because `'` is not in `\w` 
want 
üfind ==> starts with a non ASCII letter 
starting 
le11ers ==> contains digits 

缺少的話:

,to  ==> starting with a "," 
1words ==> starting with a digit 
$with ==> starting with a "$" 
相關問題