2016-09-16 78 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); 
    } 
} 
相关问题