繁体   English   中英

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

[英]How to replace a specific number of words in a string?

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

在我的代码中,我有:

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

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

我会做这样的事情

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]);
    }
}

您可以将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

只是简单的方法,只需迭代并使用数组来跟踪单词。

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
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 );
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM