-4
String htmlString ="<font color='#00ff00' size='100'> SampleText </font>"

this is a sample String where I need to change the font size from 100 to 25. I mean original font size / 4.

Is there any solution for the above problem?

The resulting string should be: "<font color='#00ff00' size='25'> SampleText </font>"

i think is it posible to do with regular expresion so here i am adding regular expression tag also htmlString.replaceAll(regularExpression, regularExpression);

4 Answers4

1

I think using Regular Expression works better.

private static String setSize(String htmlString) {
    String reg = "size='[0-9]+'";
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher(htmlString);
    while (matcher.find()) {
        String sizeString = matcher.group();
        pattern = Pattern.compile("[0-9]+");
        Matcher numMatcher = pattern.matcher(sizeString);
        if (numMatcher.find()) {
            String size = numMatcher.group();
            int realSize = Integer.parseInt(size);
            int resultSize = realSize / 4;
            String resultSizeString = "size='" + resultSize + "'";
            htmlString = htmlString.replaceAll(sizeString, resultSizeString);

        }
    }
    return htmlString;
}
Longwayto
  • 396
  • 2
  • 6
0

You can store your string in two variables font_sizeand html_templateand use the sprintffunction to "compile" your template with the values you want as follow :

char* html_template = "<font color='#00ff00' size='%d'> SampleText </font>";
int font_size = superFontSizeCalculation();
char* compiled_html = malloc(strlen(html_template));

sprintf(compiled_html, html_template, font_size);

I don't check any return of the str* functions but in real usage, you should do it.

Edit : As you changed your mind and want know java and not C, you can have the same mechanism with a small template engine by putting tags in your template string and compile by replacing the tags by your values.

For example :

static final String FONT_SIZE_TAG = "<FONT_SIZE_TAG>";
static final String FONT_COLOR_TAG = "<FONT_COLOR_TAG>";

String htmlString ="<font color='" + FONT_COLOR_TAG + "' size='" + FONT_SIZE_TAG + "'> SampleText </font>";

String compiled = htmlString.replace(FONT_COLOR_TAG, "#00ff00").replace(FONT_SIZE_TAG, String.valueOf(FONT_COLOR_TAG)); 
nesderl
  • 325
  • 1
  • 5
0

I understand the question as "is this possible with regex, and if it is, how do I do it?"

The answer is no.

Do not use regex to parse or modify html. It doesn't work. It can't mathematically work.

Check: RegEx match open tags except XHTML self-contained tags

You will always find a solution that works most of the time but fails on other examples. So unless you're okay with your solution being incomplete, regex should really not be a solution.

Community
  • 1
  • 1
Pierre Arlaud
  • 4,040
  • 3
  • 28
  • 42
  • The problem here is that the OP is simply looking for free code. Sadly [this was supplied to him](http://stackoverflow.com/a/20701563/474189). – Duncan Jones Dec 20 '13 at 10:46
  • @Duncan I understand your pain. But avoiding regex for this kind of operation is still the right call so I'm glad my answer stands in the record. – Pierre Arlaud Dec 20 '13 at 10:49
-1
   String htmlString ="<font color='#00ff00' size='100'> SampleText </font>";
            int pos1=htmlString.indexOf("size='");
            if(pos1!=-1){
                int pos3=pos1+6; /*6 is the length of size=' */
                String tempString=htmlString.substring(pos3, htmlString.length());
                int pos2=tempString.indexOf("'");
                if(pos2!=-1){
                    String sizeString=htmlString.substring(pos3, pos3+pos2);
                    String stringToReplace=htmlString.substring(pos1,pos3+pos2+1);
                    int size=Integer.parseInt(sizeString);
                    int newSize=size/4;
                    String newString="size'"+newSize+"'";                        
                    htmlString=htmlString.replace(stringToReplace, newString);
                }
            }
Fathah Rehman P
  • 8,401
  • 4
  • 40
  • 42