开发者

Need to trim a string till end after a particular combination of characters

开发者 https://www.devze.com 2023-01-02 10:15 出处:网络
I need help in trimming everything in my string till end after it encounters the first \"\\0\" So: \"test\\1\\2\\3\\0\\0\\0\\0\\0\\0\\0\\0\\0_asdfgh_qwerty_blah_blah_blah\"

I need help in trimming everything in my string till end after it encounters the first "\0"

So:

"test\1\2\3\0\0\0\0\0\0\0\0\0_asdfgh_qwerty_blah_blah_blah"

becomes

"test\1\2\3"

I am usin开发者_Python百科g c#. Help would be greatly appreciated.

Thanks.


How about:

string s = "test\1\2\3\0\0\0\0\0\0\0\0\0_asdfgh_qwerty_blah_blah_blah";

int offset = s.IndexOf("\\0");
if (offset >= 0)
    s = s.Substring(0, offset);


if (someString.Contains("\\0"))
    someString = someString.Substring(0, someString.IndexOf("\\0"));


Find the position of the first occurrence of "\0" and make another string which is a substring of you original from start to the position of the first occurrence.


somestring=Regex.split(somestring,"\\0")[0];


String.Substring(0, String.IndexOf("\0"))


if (origString.IndexOf(@"\0") != -1) {
   newString = origString.Substring(0, origString.IndexOf(@"\0");
} else {
    newString = origString;
}


The match value returned by the following regex should be what you're after as well (as alternative to the substring method), basically it begins at the string start and as long as the next two characters aren't \0 it expands the match:

^(?:(?!=\0).)+
0

精彩评论

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