String.trim doesn't work

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.

I agree, I’m just testing OP code and it works here.

I just don’t like a thread with title “XYZ doesn’t work” and talk about the docs or better code to do things (sending the message that in fact XYZ is broken) when XYZ is working. That’s all.

maybe the original text is “data-n-head=”
and the trim search for data-n-head= not “data-n-head=”
i mean this quotes

you could output this Pos or look the value at break point.
or check if it is a real minus char

You should inspect your data. If it works sometimes, an not in others, something is not matching. GIGO: Garbage In, Garbage Out. :smile:

I don’t understand how you can write that. It is MyString2 = MyString1.Trim or MyString2 = Trim(MyString1) but what is MyString2 = MyString1.Trim(MyString3)???

It removes the chars in MyString3 from the start and end of MyString1 and stores the result in MyString2.

1 Like