-1

** From this given string we need to replace all html tags as blank which located into the double curly braces'{{''}}'. **

String Temp = '';String temp2 = '';String temp3 = '';String temp4 = null;String temp5 = null;String splitVal = '';
String str = '<html><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"></head><body style="background-color:#ffffff;padding:72pt 72pt 72pt 72pt;max-width:468pt"><p style="padding-top:15pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">Id: {{Id}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">Opportunity accountid: {{Opportunity_accountid}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">Opportunity: {{Opportunity}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="font-weight:700">Opportunity: {{</span><span style="font-style:italic">Opportunity</span><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="font-weight:700">Opportunity: {{</span><span style="text-decoration-skip-ink:none;-webkit-text-decoration-skip:none;font-weight:700;text-decoration:underline">Opportunity</span><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="font-weight:700">Opportunity: {{</span><span style="color:#4a86e8;font-weight:700">Opportunity</span><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">}}</span></p></body></html>';
integer count = str.countMatches('{{');

for(Integer i=1; i<=count; i++)
{
    Temp = str.substringBetween('{{', '}}');
    system.debug('temp-->'+temp);
    temp5 = Temp.replaceAll('\\<.*?\\>','');
                system.debug('temp5-->'+temp5);
    
}
7
  • 1
    Welcome to the community. This community is not used to simply provide answers, but to help with solving concrete issues. Please modify your question to describe what steps have you taken so far to resolve it. – ytiq Jun 21 at 9:52
  • @ytiq Hi I try to replace the html tags from the given string but it shows on 1 value which exist on curly braces . – Singh Jun 21 at 10:01
  • @ytiq as you said i modified my question and add my steps which i have to try to resolve this – Singh Jun 21 at 10:03
  • and what is your debug output? 1. You do not remove {{ }} from the string, so I think it takes same substring each time 2. Your Regex only matches opening tag, but not closing – ytiq Jun 21 at 11:43
  • This question is an almost exact duplicate of salesforce.stackexchange.com/questions/347164/… - but I can't flag this as duplicate since the other question does not have an upvote or an answer. – Moonpie Jun 21 at 11:51
0

You can find your substrings and replace them with str.stripHtmlTags()

String Temp = '';String temp2 = '';String temp3 = '';String temp4 = null;String temp5 = null;String splitVal = '';
String str = '<html><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"></head><body style="background-color:#ffffff;padding:72pt 72pt 72pt 72pt;max-width:468pt"><p style="padding-top:15pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">Id: {{Id}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">Opportunity accountid: {{Opportunity_accountid}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">Opportunity: {{Opportunity}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="font-weight:700">Opportunity: {{</span><span style="font-style:italic">Opportunity</span><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="font-weight:700">Opportunity: {{</span><span style="text-decoration-skip-ink:none;-webkit-text-decoration-skip:none;font-weight:700;text-decoration:underline">Opportunity</span><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">}}</span></p><p style="padding-top:7.5pt;color:#000000;padding-left:0;font-size:11pt;padding-bottom:15pt;line-height:1.0;margin-right:15pt;margin-left:15pt;font-family:&quot;Arial&quot;;margin-top:0;margin-bottom:0;text-align:left;padding-right:0"><span style="font-weight:700">Opportunity: {{</span><span style="color:#4a86e8;font-weight:700">Opportunity</span><span style="color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">}}</span></p></body></html>';
integer count = str.countMatches('{{');

System.debug('###str.length() = ' + str.length());
for(Integer i=1; i<=count; i++)
{
    Integer i1 = str.indexOf('{{');
    Integer i2 = str.indexOf('}}');
    if (i1 == -1 || i2 == -1) {
        break;
    }
    Integer length = i2 - i1;
    System.debug('###i1 = ' + i1);
    System.debug('###i2 = ' + i2);
    String s1 = str.substring(i1, i2 + 2);
    String s2 = str.substring(i1  + 2, i2).stripHtmlTags();

    System.debug('###s1` = ' + s1);
    System.debug('###s2 = ' + s2);
    str = str.replace(s1, s2);
    System.debug('###str = ' + str);

}
7
  • In given output its give me only Id but if you beautify the html string yo can see the other names like opportunity . So I want all values which have between the curly braces but the for loop stuck on first curly braces value. – Singh Jun 21 at 11:55
  • try it again, updated the answer – ytiq Jun 21 at 11:57
  • as I said in your try you weren't removing {{ }} from your string – ytiq Jun 21 at 11:58
  • as it is working now, please accept my answer, so the question can be closed – ytiq Jun 21 at 12:08
  • In the given output it remove all the html tags between the curly braces and also remove the curly braces So i want in my desired output like all html tags are removed the remaining value shown in curly braces i don't want to remove curly braces. – Singh Jun 21 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.