简体   繁体   中英

replacing all occurance of a substring from a string in java

i have a string

String s = "#@#:g#@# hello #@#:(#@# How are You";

#@#:g#@# is the code for an emoicons. similar next #@#:(#@# is another code.

now my string has several code starting with #@# and ending with #@#. Now there is a requirment to me replacing all occurance of substring starting with #@# and ending with #@# with another string "(emotions)".

Input String = "#@#:g#@# hello  #@#:(#@# How are you";
Output String  = "(emotions) hello (emotions) How are You".

i have tried this code

System.out.println("Original String is = "+s);
    if(s.contains("#@#"))
    {
        //int startIndex = s.substring(beginIndex, endIndex)
        int index = s.indexOf("#@#");
        while (index >= 0) {
            System.out.println("index is == "+index);
            arr.add(index);
            index = s.indexOf("#@#", index + 1);
        }

        for(int i = 0 ; i<arr.size() ; i=i+2)
        {
            int startIndex = arr.get(i);
            int secondIndex = arr.get(i+1);
            System.out.println("StartIndex is == "+startIndex);
            System.out.println("SecondIndex is == "+secondIndex);

            String s1 = s.substring(startIndex,secondIndex+3);

            System.out.println("String to be replaced is == "+s1.toString());
            s.replace(s1, "(emotions)");             //\"(emotions)\"
            System.out.println("String  == "+s.toString());
        }
    }

    System.out.println("Final String is == "+s.toString());
}

Please help me.

Use String.replaceAll :

String input = "#@#:g#@# hello  #@#:(#@# How are you";
String output = input.replaceAll("#@#.*?#@#", "(emotions)");
System.out.println(output); // (emotions) hello  (emotions) How are you

The first argument passed to replaceAll is a regular expression for "#@#" followed by any characters followed by "#@#".

Try String.replace

String s = "#@#:g#@# hello #@#:(#@# How are You";
String output = s.replace("#@#", " ");
System.out.println(output);

EDIT

public String replace(String s){
    if(s.contains("#@#:g#@#")){
       s.replace("#@#:g#@#", "requiredString1");
    }
    if(s.contains("#@#:)#@#")){
       s.replace("#@#:)#@#", "requiredString2");
    }   

//add other required patterns here
       return s;
}
  • First, you have to split for spaces into an array.
  • Create a new String as output.
  • Then, iterate over the array and if item(i).contains("#@#") -> output+="(emotions) " else output += item(i)"#@#"

Hope it helps

Use the split method.

String line = "#@#:g#@# hello #@#:(#@# How are You";
String tokens[] = line.split("#@#");
for(String token: tokens){
    System.out.println(token);
    if(token.equals(":g")){
         // replace with emoticon
    }
    ....
}

Try like this:--

String s = "#@#:g#@# hello #@#:(#@# How are You";
        String s1=s.replace("#@#:g#@#", "(emotions)");
        String s2=s1.replace("#@#:(#@#", "(emotions)");
        System.out.println(s2);

This will replace "#@#{xx}#@#" with "(emotions)". {xx} is strictly any 2 characters.

"#@#:g#@# hello  #@#:(#@# How are you".replaceAll("#@#.{2}#@#", "(emotions)");

Input : #@#:g#@# hello  #@#:(#@# How are you
Output: (emotions) hello  (emotions) How are you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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