I am trimming a string data type that has a value of \t in C# .NET 3.5. I have used the method .SubString() and converted it on a char data type but still it won't work.
It returns "\" as a char.
I wanted to get the value of "\t" instead of \t or "\";
Here's my code:
string sample = 开发者_如何学运维"\\t";
char val = Convert.ToChar(sample.Substring(0, 1));
Thanks in advance.
"\\t"
specifies a backslash (\\
) followed by a t (t
). Since Substring(0, 1)
returns the first character, it would return the backslash.
Instead of "\\t"
, just use "\t"
, with one slash.
You can use string literals to reduce this confusion.
string a = "hello \t world"; // hello world
string b = @"hello \t world"; // hello \t world
It kind of sounds like you are trying to remove occurrences of tabs (\t
) from a string? If that's the case, you can use the Trim function: s.Trim('\t')
.
There's also TrimStart and TrimEnd, to remove from the start only or from the end only. If you want to remove all occurrences of tabs from the string, you can do s = s.Replace("\t",string.Empty)
.
精彩评论