簡體   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