String.trim doesn't work

hello,

i bumping my head that in some cases string.trim() doesn’t work

CurMeta = “data-n-head=”+“content=”“SBF borrowed $546M from Alameda to fund Robinhood”“”

curRemove = “data-n-head=”
Pos = CurMeta.indexof(curRemove)

if Pos <> -1 then
// it goes here, but next line doesn’t remove string
CurMeta = CurMeta.Trim(curRemove)
end if

if i hardcode the string in an example file it works
is there’s some cases trim doesn’t work as expected ? thanks

I do not saw any space character to remove…

1 Like

added
but if if hardcode the string in plain text it works
when i scan an array, pos <> -1, so it finds it, but trim doesn’t trim anything

Trim

Trim only unicode “whitespace” characters… or the LR is wrong

What are you expecting Trim() to do ? Your string curRemove has no whitespace at the start or end of it, so Trim() does nothing, as expected.

If you want to remove “curRemove” from CurMeta, trim won’t work for you.

I understand you want trim removes argument from variable. It not works that way.

You must use replace (or String.replace) and replace curRemove with nothing (ie: “”) if you try to do what I understood.

1 Like

seems trim only remove a single character at the beginning and end? .Trim(“A”,“B”)
unfortunately argument should be a char not a string as input

1 Like

Can I suggest that @MarkusR and @PhilippeP read the documentation for trim() ?

2 Likes

The Free® Link was provided earlier by myself…:innocent:

Markus is correct following the docs, reading or not.

Correct, the docs states chars, not strings. But the real behavior can be different.

Var curMeta As String = "content=""SBF borrowed $546M from Alameda to fund Robinhood"""

Var removePattern As String = "^\s*data-n-head\s*=\s*" // only at beginning and can have spaces around

Var re As New RegEx
re.SearchPattern = removePattern
re.ReplacementPattern = ""

// *** Tests ***

Var s As String = re.Replace("data-n-head="+curMeta)

If s <> curMeta Then MessageBox("Assertion 1 Failed")

s = re.Replace(" data-n-head = "+curMeta)

If s <> curMeta Then MessageBox("Assertion 2 Failed")

curMeta = "content=""SBF borrowed the string 'data-n-head=' to fund Robinhood"""

s = re.Replace("data-n-head="+curMeta)

If s <> curMeta Then MessageBox("Assertion 3 Failed")

MessageBox "If no assertion has been fired, tests were OK"
Quit
1 Like

OK. Chalk one up to the new documentation then, which, if you put trim in the search box, gives you Text.Trim and not string.Trim.

i used the link in the forum header.

Odd. That’s what I just did. Still gives me Text.Trim.

If they can prioritize what to show first, String should supersede Text.

https://documentation.xojo.com/api/data_types/string.html#string-trim

Docs are full of little mistakes, this part for instance:

image

Based on what they say, I guess they intended to show something like:

Var source As String = "<Hello World>"
Var result As String = source.Trim("<",">") // Returns "Hello World"

I tried the following code and it works as you expected:

Var CurMeta as String = "data-n-head="+"content=""SBF borrowed $546M from Alameda to fund Robinghood"""
Var curRemove as string = "data-n-head="
var pos as integer = CurMeta.IndexOf(curRemove)

if pos <> -1 then
  Break
  CurMeta = CurMeta.Trim(curRemove)
end if
Break

First break shows data-n-head= in CurMeta
image

Second break, is gone:
image

Maybe you have invisible characters and that is why Trim is not working for you?

1 Like

yes alberto example works in Xojo 2021r3.1 (Windows 10)
trim with character input is a new feature.

That’s why I made this → code above instead.

It handles some cases as tabs and extra spaces, and only removes the proper heading string.

Or he can be affected by behavior changes.

1 Like

And that’s great.
I’m just testing what the OP said that doesn’t work and it works.
Trim is not the problem here.

Maybe Trim is not the best approach for his problem.