Android Question Replace several words in a long string?

Discussion in 'Android Questions' started by TpS, Aug 27, 2015.

  1. TpS

    TpS Member Licensed 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:
    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:
    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! :)
  2. imbault

    imbault Well-Known Member Licensed User

    You can try that, il will replace all width="nnn" with width="324"

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

    Sub RegexReplace(Pattern As String, Text As String, Replacement As StringAs 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
    DonManfred likes this.
  3. TpS

    TpS Member Licensed User

    Perfect, worked just the way I wanted!
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice