Android Question Replace several words in a long string?

TpS

Member
Licensed User
Longtime User
My app receives html code from an API and is later displayed in a Webview.
It sometimes contains images with different width settings, often to big so it doesn´t show properly in my app.

That means that my String can contain one, two, three or more snipplets looking like this: width="612" but the "612" can be any number telling that image width.
I put this in my code:
B4X:
post_content = post_content.Replace(post_content.SubString2(post_content.IndexOf("width"), post_content.IndexOf("width")+11),"width=""324")
Works good for the first image, but number two and below still have it´s original size.

Is there any way to replace using wildcards or something similar? Thinking of something like this:
B4X:
post_content = post_content.Replace("width=""***","width=""324")

Or how can I solve the problem with replacing all width="xxx" in my String?
I assume a loop or something could solve it but my head is stuck right now.

Please help! :)
 

imbault

Well-Known Member
Licensed User
Longtime User
You can try that, il will replace all width="nnn" with width="324"

B4X:
post_content = post_content.RegexReplace( "width=""(\d+)", post_content, "width=""324")


Sub RegexReplace(Pattern As String, Text As String, Replacement As String) As String
   Dim m As Matcher
   m = Regex.Matcher(Pattern, Text)
   Dim r As Reflector
   r.Target = m
   Return r.RunMethod2("replaceAll", Replacement, "java.lang.String")
End Sub
 
Upvote 0
Top