如何替换字符串中特定数量的单词?

时间:2016-09-16 22:55:27

标签: java string

我的代码尝试做的是在换行符上打印字符串的每个单词。但是,我必须将其限制为仅前五个字。另外,我必须为这五个单词中的每一个添加“第一个单词:单词”等等。 我不太确定如何获得我想要的结果。

在我的代码中,我有:

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

任何帮助或指导都将不胜感激,谢谢!

4 个答案:

答案 0 :(得分: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]);
    }
}

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分: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 );
    }
}