-1

I am trying to find a substring in a string. Then remove already present <b> tags from the original string & add <b> tag before & after the substring existence in the original string.

I need help with this, what I've tried so far is below

String originalString = "<b>ProLiantDL380CltrG2</b> HWSupp";
String subString = "ProLia"

if (StringUtils.containsIgnoreCase(originalString, subString)) {

System.out.println("substring matched true");
System.out.println("before : " + originalString);
//Need to remove the <b> & </b> from the originalString
originalString.replace("<b>", ""); // doesn't work
originalString.replace("</b>", "");
System.out.println("after : " + originalString);
//after remove the <b> tags I need to add those tags at the start &
end index of the substring existence in the original string
like "<b>ProLia</b>ntDL380CltrG2 HWSupp"
   }
}

Output : not as expected

product matched true
before :  <b>ProLiantDL380CltrG2</b> HWSupp
after :  <b>ProLiantDL380CltrG2</b> HWSupp
underdog
  • 4,447
  • 9
  • 44
  • 89
  • [`replace`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-java.lang.CharSequence-java.lang.CharSequence-) returns a **new** string - it doesn't change the string in place (remember, strings are immutable objects). – Maroun Feb 16 '16 at 09:51
  • how do I check the start & end index of a substring in a string? – underdog Feb 16 '16 at 09:54
  • @underdog Please post your question in Google, you'll get endless results. – Maroun Feb 16 '16 at 09:55
  • You can replace `"(?:)?([^>]*)(ProLia)([^<]*)(?:)?"` with `$1$2$3` [see demo at ideone](http://ideone.com/ueOwk7) or [regexplanet](http://fiddle.re/ur9306) (click Java). Can explain more, if it's helpful. – bobble bubble Feb 16 '16 at 12:16

2 Answers2

2

In Java Strings are immutable. You need to get the string back when you modify it

originalString.replace("<b>", "");
originalString.replace("</b>", "");

Which means that you are just modifying the string and not assigning it to the original String.

Hence your code should be

originalString= originalString.replace("<b>", "");
originalString= originalString.replace("</b>", "");

Inorder to get the index you should use indexOf() which gives you start index and adding the substring length to it gives you the end index of it.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 2
    If it is the problem, why not close the question as a duplicate? Say, of [Java replaceAll() regex does not work](http://stackoverflow.com/questions/21436517/java-replaceall-regex-does-not-work). – Wiktor Stribiżew Feb 16 '16 at 09:49
  • thanks removal of the tag works fine. Now how do I append the tags to the index position of the substring in the original string? – underdog Feb 16 '16 at 09:50
  • @WiktorStribiżew Apart from it, OP wanting the indexes of the substring content. – Suresh Atta Feb 16 '16 at 09:58
  • So, your answer does not really answer the question then. – Wiktor Stribiżew Feb 16 '16 at 10:01
  • @WiktorStribiżew If you read the whole answer after the edit, you may notice the new other things I added – Suresh Atta Feb 16 '16 at 10:05
  • *add `` tag before & after the substring existence in the original string* - where in your answer do you insert the tags? – Wiktor Stribiżew Feb 16 '16 at 10:12
  • @WiktorStribiżew I am not a coding service to him to write everything in hand. I just gave tips to get the indices. OP should insert tags based on them. Feel free to downvote if you disagree and add an answer if you understand the question. – Suresh Atta Feb 16 '16 at 10:14
  • 1
    My opinion is that when you write an answer, you should: 1) explain where OP is wrong, 2) your solution in the form of working code, 3) demo to show your code works (if possible online). This is my preferred style. My aim is not to downvote, I dislike downvoting, it always makes me sad. – Wiktor Stribiżew Feb 16 '16 at 10:19
1

you should use stringBuilder to perform these conditions and its the fastest way to do it, Try the below code,

import com.sun.org.apache.xml.internal.utils.StringComparable;
import com.sun.xml.internal.ws.util.StringUtils;

public class matchSubstring {

    public static void main (String[]args){

    String originalString = "<b>ProLiantDL380CltrG2</b> HWSupp";
    String subString = "ProLia";

    if (originalString.contains(subString)) {
    System.out.println("substring matched true");
    System.out.println("before : " + originalString);
    //Need to remove the <b> & </b> from the originalString
    originalString= originalString.replace("<b>", ""); 
    originalString=originalString.replace("</b>", "");

    StringBuilder strb=new StringBuilder(originalString).insert(subString.length(),"</b>");
    strb.insert(0,"<b>");

    System.out.println("after : " + strb);
}
}}

output:

substring matched true
before : <b>ProLiantDL380CltrG2</b> HWSupp
after concat: <b>ProLia</b>ntDL380CltrG2 HWSupp
karthick23
  • 1,313
  • 1
  • 8
  • 15