You need to trim the string to the end after a certain combination of characters

I need help trimming everything in my line to the 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 using C #. Help would be greatly appreciated.

Thank.

+3
source share
7 answers

What 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);
+6
source
if (someString.Contains("\\0"))
    someString = someString.Substring(0, someString.IndexOf("\\0"));
+5
source

"\ 0" , .

+1

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

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

The match value returned by the following regular expression should be what you also are (as an alternative to the substring method), basically it starts at the beginning of the line and until the next two characters are \0it it expands the match:

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

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


All Articles