开发者

Trimming a string help

I have an object array that is being passed a开发者_运维知识库nd then split up into different variables. The problem is, one of the variables is supposed to be a number/a string, whatever, but it is arriving with all these funky characters on the beginning and end of the number. I set up a break point so I could see the value, it is this: "\"91\""

or really, without the outer quotations since those are just for identification, it's \"91\"

I tried doing a trimstart and trimend like so: whateverobjarray.variablename.TrimStart('"', '/'); whateverobjarray.variablenameTrimEnd('"', '/');

I forgot if it was a / or \ slash...so it obviously didn't get the backslash...but it also didn't remove the quotes at the beginning or end either. The '\' doesn't seem to work as it still thinks anything after that is part of the characters I'm trying to get rid of.

What is the proper syntax for doing this in my situation where I just want the number...also it needs to work if the number is 1, 2, or 3 digits. Do I have to cast it as a string or some type of number first? HELP!!! It's driving me crazy!

edit I also tried this char[] trimArray = { '"', '\'', '/' }; whateverobjarray.variablename.TrimStart(trimArray); whateverobjarray.variablename.TrimEnd(trimArray);

Didn't take anything off either.


First of all, I think the backslash is just escaping the quotes, so you'll want to trim \". Secondly, since strings are immutable, calling variablename.TrimStart() doesn't actually change the value of that variable. You have to say:

obj.MyString = obj.MyString.Trim('"');


These look like escaped characters, so each \" is a single character. You could do a replace to remove the characters. However, your best bet is to get a handle on character escaping in C# and it may all click into place.

try

variablename.Replace("\"","");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜