TrimEnd with ToCharArray

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
Why am I getting unexpected results here? This is VB.NET 2005 from my
Immediate Window. See 2nd line.

?"test.trim".TrimEnd("trim".ToCharArray)
"test."
?"test.trim".TrimEnd(".trim".ToCharArray)
"tes"
?"test.trim".TrimEnd("t.trim".ToCharArray)
"tes"
?"test.trim".TrimEnd("st.trim".ToCharArray)
"te"

Any ideas if this is a bug or what?
 
this looks correct. you told it to trim off all trailing characters the the
array ".trim"
every character in "test.trim" after "tes" is in the array of characters to
trim.
You would get the same thing if your string was
"test.trimmiiiirrrtt.t.t."
If youre trying to trim a certain string off the end dont use trim.
Maybe try EndsWith and SubString
 
JohnMSyrasoft said:
Why am I getting unexpected results here? This is VB.NET 2005 from my
Immediate Window. See 2nd line.

?"test.trim".TrimEnd("trim".ToCharArray)
"test."
?"test.trim".TrimEnd(".trim".ToCharArray)
"tes"
?"test.trim".TrimEnd("t.trim".ToCharArray)
"tes"
?"test.trim".TrimEnd("st.trim".ToCharArray)
"te"

Any ideas if this is a bug or what?

Not a bug, just a mistaken understanding of what TrimEnd does. The char
array parameter you provide is a *set* of characters to trim. It
removes the t at the end of test because it's in the set.
 
Thanks to both responses. I didn't properly understand that it was a set of
characters. I thought that ordered mattered in some way but now I
understand. Thanks very much for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top