Kuldeep Sakhiya
Kuldeep Sakhiya

Reputation: 3252

replace text with string

I have to replace "font-size" value in my HTML text with my required text size for my android app.

I am getting

font-size: 26.2101px;

font-size: 0.9em

font-size: 31px;

I want to replace "font-size" attribute with value "16px". How can I achieve this?

Upvotes: 2

Views: 766

Answers (4)

ninja.coder
ninja.coder

Reputation: 9648

If I understood your problem correctly then you are probably looking for something like this:

public static void main (String[] args)
{
    /* Text To-Be Replaced */
    String myTextString = "font-size: 26.2101px;\nfont-size: 0.9em\nfont-size: 31px;";
    /* Regex Replace */
    System.out.println(myTextString.replaceAll("font-size: [0-9]+px;","font-size: 16px;");
}

Output:

font-size: 26.2101px;
font-size: 0.9em
font-size: 16px;

Upvotes: 1

N J
N J

Reputation: 27515

This is possible using RegEx

(font-size: \\d+.?\\d+((em)?(px)?))

Example

     String s="font-size: 26.2101px;";
     String regEx="(font-size: \\d+.?\\d+((em)?(px)?))"; 
     s=s.replaceAll(regEx,"16px");

Upvotes: 1

Mustansar Saeed
Mustansar Saeed

Reputation: 2790

You can use Regular Expression to replace your text as

String htmlString = Html.fromHtml("yourHtmlString").toString();
String regex = "font-size: \\(\\[0-9\\]*\\(.*\\)\\[0-9\\]+px\\);";
htmlString.replaceAll(regex, "font-size: 30px");

Hope this helps.

Upvotes: 2

Jaco B
Jaco B

Reputation: 1043

You should be able to achieve this with a regular expression.

Something in the form of font-size: [0-9].?[0-9](px|em)

For more details on regular expressions you can check out this page:

To Test this expression, you can go here

Upvotes: 2

Related Questions