2016-09-16 86 views
0

我的代碼試圖做的是在換行符上打印每個字符串。但是,我必須將其限制在前五個單詞中。另外,對於這五個單詞中的每一個,我必須添加「第一個單詞:單詞」等等。 我不太清楚如何去獲得我想要的結果。如何替換字符串中特定數量的單詞?

在我的代碼,我有:

int space = sentence.indexOf(" "); 
    sentence = sentence.substring(0,space) + "\n" + sentence.substring(space+1);   
    System.out.println(sentence.replaceAll("\\s+", "\n")); 

任何幫助或指導,將不勝感激,謝謝!

+1

要拆分串入詞,你可以使用'string.split(「」)'然後遍歷結果數組。 – Beethoven

+0

http://stackoverflow.com/questions/7577543/how-to-replace-a-word-in-a-string –

+0

OP有什麼作用。他們需要做的是爲這個單詞添加一個循環和一個臨時變量,而不是試圖在原地建立字符串。 – markspace

回答

1

我會做這樣的事情

char[] sentanceChars = sentance.toCharArray(); 
StringBuilder sb = new StringBuilder(); 
int wordIdx = 1; 
for(int i=0; i<sentance.length(); i++) { 
    if(sentanceChars[i] == ' ') { 
     System.out.println("word "+ (wordIdx++) +"="+ sb.toString()); 
     if(wordIdx == 6) { 
      break; 
     } 
     sb = new StringBuilder(); 
    } else { 
     sb.append(sentanceChars[i]); 
    } 
} 
+0

是的,正好... – blue

+0

不錯。 Upvoted它。 – c0der

0

你可以分開你的Token字符串,並與其他字符串比較,如果符合replaces的字符串,然後打印出第5個字與請求的格式 和打印換句話說頂端5的外側可以用一個StringBuilder

StringBuilder bu = new StringBuilder(); 
String str = "This is String As a Compare DAD ADAS DAS"; 
StringTokenizer st = new StringTokenizer(str); 
    int x =0; 
    while (st.hasMoreElements()) { 
    String val = (String) st.nextElement(); 
    x+=1; 
    if(val.equals("is"))val="NewValue"; //change word to other value 
    if(x<=5)System.out.println(" WORD N° " + x + " " +val); 
    else 
     bu.append(val).append(" "); 
    } 
      System.out.println(bu.toString()); 

輸出繼電器

WORD N° 1 This 
WORD N° 2 NewValue 
WORD N° 3 String 
WORD N° 4 As 
WORD N° 5 a 
Compare ABC DFG AHG 
0

只迭代和使用數組來跟蹤詞的一個非常簡單的方法...

import java.util.Arrays; 

public class WordSplit { 

    public static void main(String[] args){ 
     printStrings("This is a test string", 2); 
    } 


    public static void printStrings(String sentence, int skip){ 
     String[] splitSentence = sentence.split(" "); 
     String[] afterSplit = Arrays.copyOfRange(splitSentence, skip, splitSentence.length); 
     int c = 0; 

     for(int i=0; i<skip; i++){ 
      System.out.println(ordinal(i+1)+" word: "+splitSentence[i]); 
     } 
     System.out.print("The rest of the sentence: "); 
     for(String s: afterSplit){ 
      System.out.print(s+" "); 
     } 

     System.out.println(); 
    } 

    public static String ordinal(int i) { 
     String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" }; 
     switch (i % 100) { 
      case 11: 
      case 12: 
      case 13: 
       return i + "th"; 
      default: 
       return i + sufixes[i % 10]; 
     } 
    } 

} 

輸出:

1st word: This 
2nd word: is 
The rest of the sentence: a test string 
0
public static void main(String[] args){ 

    final String[] prefix = {"st", "nd", "ed","th", "th"}; 
    final String SPACE =" ", NL = "\n"; 
    String sentance ="A sentence with five word or more"; 
    int counter = 0; 

    for(String s: sentance.split(SPACE)){ 

     if(++counter > prefix.length) break; 
     System.out.println(counter + prefix[counter-1] + " WORD: " + s + NL); 
    } 
} 
相關問題