Why doesn't String.Trim trim the semicolon?

I am trying to trim a string using String.Trim

split[2].Trim(';') 

but I get the result:

 System; 

instead

 System 

I also tried using

 split[2].TrimEnd(';') 

but it still returns the same result.

Also if i do

 split[2].Trim('S', ';') 

I get

 ystem; 

I am really confused why this is happening. Maybe because the semicolon is not the last character in the string?

Here is the complete code:

 string line = @" using System; using System.Windows.Forms; class HelloWorld { static void Main() { #if DebugConfig Console.WriteLine("WE ARE IN THE DEBUG CONFIGURATION"); #endif Console.WriteLine("Hello, world!"); DialogResult result; result = MessageBox.Show("Itsyeboi", "Yup", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { Console.WriteLine("Yes"); Console.ReadLine(); } else { Console.WriteLine("No"); Console.ReadLine(); } } }" string[] split = line.Split(' ', '\n'); while (true) { if (split[counter4] == "using") { richTextBox1.Text = split[1].Trim(';'); break; } else { richTextBox1.Text = line; break; } } 

FYI I recently saw a comment that noticed that my while loop makes no sense. True, but this is "WIP", not the final version.

+5
source share
2 answers

Replace the following line:

 string[] split = line.Split(' ', '\n'); 

Wherein:

 string[] split = line.Replace("\r\n", "\n").Split(' ', '\n'); 

This will replace any carriage returns with only newlines, eliminating the possibility that the trailing \r may cause your problem.

+3
source

If this answer is incorrect, I will delete it. Since information is sparse, this is only a wild guess.

Insert

 split[2].Trim() 

right in front

 split[2].Trim(';') 

most likely will do the trick.

If not, but you know the last char is ; and you only want to get rid of it, you can just slice the array. This would probably be more efficient since you would not need to compare any characters, although this is most likely done at least two times if you use TrimEnd or three times if you Trim

0
source

Source: https://habr.com/ru/post/1261809/


All Articles