Forum Discussion

jeffrey_crowley's avatar
jeffrey_crowley
Contributor
12 years ago

aqString.Trim() is not removing nbsps as whitespace

I'm trying to trim whitespace from a string using aqString.Trim() that I pulled from an HTML element as .innerText. The problem seems to be that it contains a <space><nbsp> at the end of the string so Trim() doesn't remove the nbsp.



I can do this easily in javascript using <string>.trim() but TC doesn't seem to like it. Here's a simple example in jsFiddle. jsFiddle demo



... and here's an example of trying to use javascript's trim() in TC:


function test()


{


    var s = " ABC 123 ";


    Log.Message(s.trim()); 


}



Can anyone suggest a workaround? I'm trying not to have to search and replace nbsps, etc. Thanks.

  • hlalumiere's avatar
    hlalumiere
    Regular Contributor
    The only alternative is through the Replace method, same as you would do it in .Net to remove null chars.
  • dfarr's avatar
    dfarr
    Occasional Contributor
    Hi Alexei,

    Fast forwarding to now, is there any chance that aqString.Trim() will be enhanced to remove trailing &nbsp from innerText? I just discovered this limitation the hard way in attempting to compare a test value against a cell’s trimmed innerText.



    Until then, will the aqString.Replace() or an aqString substring method dispatch those pesky non-breaking spaces? It would help to know before I lose more time trying a regular expression replacement or substring comparison scheme, only to find blocking limitations with those as well.



    I am reluctant to experiment with Utilities because it was obsolete a year ago when it was suggested as an alternative.

     

    Thanks,
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Donald,



    I am not working for SmartBear, so can say nothing about whether or not aqString.Trim() will be enhanced (though I would love this).

    What I figured out recently was that while some browsers treat nbsp as a whitespace, some of them render it as Chr(160). For the needs of the given project I ended-up with

    TrimWebText = Utilities.Trim(aqString.Replace(strValue, Chr(160), ""))

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Jeff,



    I am not sure that .innerText contains explicit text like &nbsp; etc., but rather a characters with codes that differ from Asc(32). Thus, you may try not aqString.Trim(), but Utilities.Trim(). The latter is considered to be outdated, but for the majority of cases it works much better than the former one.