2016-03-22 9 views
1

Stringの問題がJavaにあります。私は大きいString\n\\nを持っている。特定のテンプレート "Chorus:"で始まる行全体を削除し、残りの行を\nまで削除したいとします。文字列テンプレートを ' n'まで削除する大きい文字列 - Java

私はStringに行き、テンプレート "Chorus:"を検索しようとします。私がそれに達すると、\nに達するまで残りの行を連結します。その後、私はmyString.reaplaceAll(subString," ")を使用し、私は同じようにStringを取得します。

マイコード:

String string = " Police voice:\n" 
         + "Attention all units attention all units\n" 
         + "We have an All Points Bulletin out on a man with green hair\n" 
         + "I repeat we have an APB on a man with green hair\n" 
         + "He's armed with a knife I repeat he's armed with a knife\n" 
         + "Proceed with caution watch your back fella's\n" 
         + "He could be coming at your girlfriend next\n\n" 

         + "Verse 1:\n" 
         + "There's a joker on the loose from the psychiatric ward\n" 
         + "His face is up on the bulletin board with a reward\n" 
         + "He'll stab you with a sword don't be fooled by his charm\n" 
         + "He's probably armed with intent to do bodily harm\n" 
         + "Ring the alarm, look for a man with green hair\n" 
         + "Check at your girl's house, he was last seen there\n" 
         + "He's has a mean stare but usually crack's jokes\n" 
         + "Good luck on your mission and guard your backs folks\n\n" 

         + "Chorus:\n" 
         + "I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Hit a bitch snake from the back, I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Stay away from him he ain't no good!\n\n" 

         + "Verse 2:\n" 
         + "We got the walkie-talkies to keep us all informed\n" 
         + "Suited up head to toe in detective uniforms\n" 
         + "Our unit storms in, we split the ghetto in sectors\n" 
         + "Locking down every block and put up metal detectors\n" 
         + "Protectors and be sure that you stand close\n" 
         + "Watch each others backs and guard your command post\n" 
         + "And most of all be advised that he's wise\n" 
         + "He could be disguised as one of your very own guys\n\n" 

         + "Chorus:\n" 
         + "I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Hit a bitch snake from the back, I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Stay away from him he ain't no good!\n" 

         + "Verse 3:\n" 
         + "I'm on a solo mission to find him personally\n" 
         + "To settle the score and beat then him mercifully\n" 
         + "For what he first did to me\n" 
         + "It's sure to be the last\n" 
         + "Following footprints with a magnifying glass\n" 
         + "To drag his lying ass back to his padded cell\n" 
         + "I'm mad as hell, on the trail of the tattle tail\n" 
         + "I heard a yell the voice sounded familiar\n" 
         + "Give me your girlfriend or I'm gonna kill ya\n\n" 

         + "Chorus:\n" 
         + "I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Hit a bitch snake from the back, I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Stay away from him he ain't no good!\n\n" 

         + "Verse 4:\n" 
         + "I followed the voice that led down a flight of steps\n" 
         + "Sneezing at dust, and swinging at the spider-webs\n" 
         + "Inside the depths of his basement\n" 
         + "I taste lint in my mouth, then the lights in the place… went!\n" 
         + "I saw my life flash in front of my eyes\n" 
         + "I felt a butcher knife slash at one of my thighs\n" 
         + "None of my guys knew where I'm at I was doomed\n" 
         + "Then I remembered the flashlight in my costume\n\n" 

         + "Chorus: Eminem\n" 
         + "I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Hit a bitch snake from the back, I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Stay away from him he ain't no good!\n\n" 

         + "Verse 5:\n" 
         + "Dealing with backstabber's there was one thing I learned\n" 
         + "They're only powerful when you got your back turned\n" 
         + "I yearned for the day that we finally met againf\n" 
         + "So I can give him a taste of his own medicine\n" 
         + "He shed his skin, then he promised to come clean\n" 
         + "I took his butcher knife and jabbed it into his spleen\n" 
         + "Cut him at the seam then dragged the fella home\n" 
         + "Beating him over the head with the telephone\n\n" 

         + "Police voice:\n" 
         + "Attention all cars, attention all cars\n" 
         + "Unit 313 has apprehended the suspect\n" 
         + "He's going back to the crazy home, I repeat\n" 
         + "He's going back to the crazy home, how about that?\n\n" 

         + "Chorus:\n" 
         + "I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Hit a bitch snake from the back, I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Stay away from him he ain't no good!\n" 
         + "I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Hit a bitch snake from the back, I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Stay away from him he ain't no good!\n" 
         + "I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Hit a bitch snake from the back, I make you think you're a best friend then\n" 
         + "Why I oughtta\n" 
         + "Stay away from him he ain't no good!"; 

     String subString = ""; // Searching for Chorus: (7 characters) sub sub string in string 

     for (int i = 0;i < string.length();i++) 
     { 
      if (string.charAt(i) == 'C') 
      { 
       subString = string.substring(i,i + 7); 
       subString += " "; // "Chorus: " 
       //System.out.println(subString); 
       if (subString.equals("Chorus:")) // Delete the row 
       { 
        i += 7; 
        while (string.charAt(++i)!= '\n') 
        { 
         //System.out.println(string.charAt(i)); 
         subString += string.charAt(i); 
        } 
        string = new String(string.replaceAll(subString, "")); 
       } 
      } 
     } 

     printString(string); 


    } 

    /** 
    * 
    * @param string to be printed on the console. 
    */ 
    public static void printString(String string) 

私は間違って何をしているのですか?どんな助けも非常に高く評価されます。

EDIT:明確にするため"abcChorus:def\n"+"gh\n"+"Chorus;ijk\n"+"lmnop"変換"gh\n"+"lmnop"

+0

'string.replace(subString、" ");'を試しましたか? – Idos

+0

@Idos私は今どこに行くのでしょうか?それを試してみました。同じ出力。 'String'に変更はありません。 – God

+0

編集後、私は答えを見直さなければならないでしょう – Idos

答えて

0

私の複雑な解決策。 Stringに "Chorus:" Stringを検索し、残りの行を連結して(\nまで)、そのサブストリングを全体でStringに置き換えました。次に、全体のStringからすべての "Chorus:"サブストリングを置き換えます。 このように私は"Chorus:"行と"Chorus: blablablalba"行に取り組んでいます。

ソリューション:

String subString = ""; // Searching for "Chorus: " (7 characters) sub string in string 


for (int i = 0;i < string.length();i++) 
{ 
    if (string.charAt(i) == 'C') 
    { 
     subString = string.substring(i,i + 8); 
     //subString += " "; // "Chorus:" 
     //System.out.println(subString); 
     if (subString.equals("Chorus: ")) // Delete the row 
     { 
      i += 8; 
      while (string.charAt(i)!= '\n') 
      { 
       //System.out.println(string.charAt(i)); 
       subString += string.charAt(i); 
       i++; 
      } 
      //System.out.println(subString); 

      string = new String(string.replaceAll(subString, "")); 
     } 
    } 
} 

string = new String(string.replaceAll("Chorus:", "")); 
2

にあなたは...言語の基本ライブラリに入ると、彼らは代わりに文字を自分でプッシュするので、何ができるかを確認する必要があり

String[] lines = string.split("\n"); // split into array at newlines 
    String result = Stream.of(lines) 
     .filter(s -> !s.startsWith("Chorus:")) // filter however you like 
     .collect(Collectors.joining("\n"));  // re-join as string with newlines 
    System.out.println(result); 
+0

混乱しています:0。私はそれを通過します。しかし、私のやり方では、何が間違っているのかを検出できますか?または、投稿された方法を試してみてください。 – God

+0

実際、私はあなたのメソッドをデバッグするのではなく、少なくともあなたのコメントの後に、コンテンツ "Chorus:"(最後にスペース!)を持つs​​ubString変数を作成し、これを "Chorus:"と比較しますスペース)。これは決して一致しません。 しかし、一般的なアドバイス:私たちは適切な図書館を持つ成熟した言語になっています。入れ子にされたループを書いていると思えば、おそらく事態が複雑になります。 – mtj

+0

また、 'ラムダ式のパラメータは、囲みスコープで定義された別のローカル変数を再宣言できません。 ' – God

1

あなたはこれを試すことができます。

String new = old.trim() 

stringから先頭と末尾のスペースを削除します。

トリミングされた文字列をどこかに保存することができます。その後、変更をそれを望む方法で連結します。

+1

私をあまり助けなかった。 – God

+0

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()これはこのコマンドのドキュメントです。これが役立ちます。私はそれを自分で使ったことはありませんでしたが、それは非常に難しいとは思わない。 – denis

+0

まだ動作しない場合は、コメントを追加してください。私はそれを自分で実行しようとします。今はできません。申し訳ありません。 – denis

1

あなたはあまりにも正規表現と再帰を利用することができます。 StringのremoveStuffメソッドを呼び出すだけです。 また、コンパイルメソッドの引数内で正規表現を変更して調整することもできます。

static Pattern pattern = Pattern.compile("Chorus([^\n]*)"); 
static Matcher matcher = pattern.matcher(string); 

public static String removeStuff(String myString, Matcher matcher) { 
    if (matcher.find() == false) { 
     return myString; 
    } 
    myString = string.replace(matcher.group(), ""); 
    return removeStuff(myString, matcher); 
} 
関連する問題