question about split method c#
-
hi to all, how can i split a string value with a seperator of many character. like this : string MyString = "abc|@def|@hij|@klm|@" after the split i want the following result : abc def hij klm best regrads and thanks in advance fady
-
hi to all, how can i split a string value with a seperator of many character. like this : string MyString = "abc|@def|@hij|@klm|@" after the split i want the following result : abc def hij klm best regrads and thanks in advance fady
Try: string test = "abc|@def|@hij|@klm|@"; char[] seperators = {'|'}; string[] splitted = test.Replace("@","").Split(seperators); Then you get an array of 5! strings, the last is empty. Greetings, Ingo ------------------------------ An bug in a Microsoft Product? No! It's not a bug it's an undocumented feature!
-
hi to all, how can i split a string value with a seperator of many character. like this : string MyString = "abc|@def|@hij|@klm|@" after the split i want the following result : abc def hij klm best regrads and thanks in advance fady
If it is the same string sequence, String.Split will do the job.
string[] myStrings = myString.Split(new char[] { '|', '@' }, StringSplitOptions.RemoveEmptyEntries);
Not that the
StringSplitOptions
is new to 2.0. If you are still in 1.1, you have 2 choices: 1- filter the empty strings when reading the array. 2- Use Regex.Split instead:string[] myStrings = Regex.Split(myString, "[\|@]+");
The regular expression means "split on | or @, which may appear one or more times. Unfortunately, the Regex one leaves an empty entry at the end, because your string finishes with the "|@" sequence. -------- "I say no to drugs, but they don't listen." - Marilyn Manson
-
hi to all, how can i split a string value with a seperator of many character. like this : string MyString = "abc|@def|@hij|@klm|@" after the split i want the following result : abc def hij klm best regrads and thanks in advance fady
Hi! What I usually do is to replace the separator strings with single characters that do not otherwise occurr inside the string and then perform a regular Split. Something like this:
string MyString = "abc|@def|@hij|@klm|@";
string MyNewString = MyString.Replace("|@", "\0");
string[] Parts = MyNewString.Split('\0');You should get the expected result. You have to be careful, though: Since your string ends with a separator, you'll get an additional empty string as last entry in your result array! Regards, mav
-
Hi! What I usually do is to replace the separator strings with single characters that do not otherwise occurr inside the string and then perform a regular Split. Something like this:
string MyString = "abc|@def|@hij|@klm|@";
string MyNewString = MyString.Replace("|@", "\0");
string[] Parts = MyNewString.Split('\0');You should get the expected result. You have to be careful, though: Since your string ends with a separator, you'll get an additional empty string as last entry in your result array! Regards, mav
mav.northwind wrote:
string MyNewString = MyString.Replace("|@", "\0");
Funny, I think I mistakenly did that the other day and it blew up in my face! :confused:
-
mav.northwind wrote:
string MyNewString = MyString.Replace("|@", "\0");
Funny, I think I mistakenly did that the other day and it blew up in my face! :confused:
I must admit that I wasn't sure that '\0' would behave nicely until I tried. But now that I actually tried it there's no real reason why it shouldn't work - .NET strings aren't \0-terminated, so \0 should be a character like every other. Only difference is that you'll have a hard time trying to insert a \0 manually in your text :) Regards, mav
-
I must admit that I wasn't sure that '\0' would behave nicely until I tried. But now that I actually tried it there's no real reason why it shouldn't work - .NET strings aren't \0-terminated, so \0 should be a character like every other. Only difference is that you'll have a hard time trying to insert a \0 manually in your text :) Regards, mav
mav.northwind wrote:
.NET strings aren't \0-terminated
Try tell that to Console.WriteLine() ;P