2016-09-16 3 views
0

私のコードがしようとしていることは、改行に文字列の各単語を表示することです。しかし、私はそれを最初の5つの単語に限定しなければなりません。さらに、これらの5つの単語のそれぞれに対して、「最初の単語:単語」などを追加する必要があります。 私は望む結果を得るためにどうやって行くのかは分かりません。私のコードで文字列内の特定の数の単語を置き換えるにはどうすればよいですか?

、私が持っている:、任意のヘルプやガイダンスがいただければ幸いです

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

私はそれがreplaces文字列を満たした後、要求されたフォーマット とし、第1〜第5の言葉を印刷している場合は、あなたのToken文字列を分離して、いくつかの他の文字列と比較することができ、この

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

ような何かをするだろう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()); 

出力リレー

を使用することができ、トップ5の外に他の単語を印刷します
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); 
    } 
} 
関連する問題