Home > Back-end >  How to put new string between commas in original string
How to put new string between commas in original string

Time:05-23

I have this original string and I want to insert new string between two dots of original string. I did it this way, but having errors.

String originalString ="asdASfasdlpe.hereNeedToPutNewString.asdasfdfepw";
String stringForReplace = "NewString";
String new = originalString.replace(originalString.substring(originalString.indexOf(".")   1), stringForReplace);
it gives me: "asdASfasdlpe.NewString"

Result should be: "asdASfasdlpe.NewString.asdasfdfepw"

CodePudding user response:

String originalString ="asdASfasdlpe.hereNeedToPutNewString.asdasfdfepw";
String stringForReplace = "NewString";
String a[]=originalString.split("[.]");
String newString="";
if(a.length==3) {
newString=originalString.replace(a[1], stringForReplace);
}
System.out.println(newString);

Or with ternary operator:

newString=(a.length== 3 ? originalString.replace(a[1], stringForReplace):null);
System.out.println(newString);

CodePudding user response:

One shorter solution is to use regex with a lookahead and lookbehind

String replaced = originalString.replaceAll("(?<=\\.). (?=\\.)", stringForReplace);

CodePudding user response:

The problem with your code is due to using this particular piece of code:

originalString.substring(originalString.indexOf(".") 1)

The reason is that indexof() function will only give the index on which "." was found on, and substring will only know where to start taking the substring from, but it wouldn't know where to end it.

Try this:

String originalString ="asdASfasdlpe.hereNeedToPutNewString.asdasfdfepw";
String stringForReplace = "NewString";
String newString = originalString.replace(originalString.split("[.]", 3)[1], stringForReplace);
System.out.println(newString);

The split function in this piece of code will break the whole string by "." and you will have the string you want to replace available to you.

originalString.split("[.]", 3)[1]

CodePudding user response:

You could try the following:

public static void main(String[] args) {
    String originalString ="asdASfasdlpe.hereNeedToPutNewString.asdasfdfepw";
    String stringForReplace = "NewString";
    String newStr = originalString.replaceAll("(?<=\\.).*(?=\\.)", stringForReplace);
    //using lookahead and lookbehind regex
    String newStr2 = originalString.replaceAll("\\..*\\.", "." stringForReplace ".");
    System.out.println(newStr);
    System.out.println(newStr2);
}

One option uses lookahead and lookbehinds, you could opt to not use that if it is not supported.

Output:

asdASfasdlpe.NewString.asdasfdfepw
asdASfasdlpe.NewString.asdasfdfepw

CodePudding user response:

Here You go:

String new = originalString.replace(originalString.substring(originalString.indexOf(".")   1), stringForReplace) originalString.substring(originalString.indexOf(".",originalString.indexOf(".") 1),originalString.length());

What I have done is adding the resultant string to the new String.indexOf function takes another argument too, which is the position the search will start

  •  Tags:  
  • java
  • Related