Split function in c#?
-
See String.Split and Regex.Split. Kant wrote: Actually she replied back to me "You shouldn't fix the bug. You should kill it"
-
See String.Split and Regex.Split. Kant wrote: Actually she replied back to me "You shouldn't fix the bug. You should kill it"
cheers , although i'm struggling with it a bit, if i want to seperate the first space in a row of text, how would it be done? all i can find is info on arrays of stuff ( eg: i want to split this line "hello i'm a test" , so that all i get is "i'm" ) but all i've managed to find is some really complex stuff on msdn about splitting whole lines:~ in vb6 / vb.net i've always been able to do this... Dim text as string text = Split("hello i'm a test",chr(32))(1) /// bringing back the "i'm" bit any ideas?
-
cheers , although i'm struggling with it a bit, if i want to seperate the first space in a row of text, how would it be done? all i can find is info on arrays of stuff ( eg: i want to split this line "hello i'm a test" , so that all i get is "i'm" ) but all i've managed to find is some really complex stuff on msdn about splitting whole lines:~ in vb6 / vb.net i've always been able to do this... Dim text as string text = Split("hello i'm a test",chr(32))(1) /// bringing back the "i'm" bit any ideas?
Both the C# version and the VB6 version return an array, you are just accessing a specific element of that array in your example. This should work:
string word = "hello i'm a test".Split(' ')[1];
To be more verbose, you are setting the variable word to the first element in the array returned by calling theSplit
method on the string "hello i'm a test". The large downfall with the .NET version of Split is that it only splits on characters not strings. I don't think this is a problem with Regex's but I don't know for sure. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him -
cheers , although i'm struggling with it a bit, if i want to seperate the first space in a row of text, how would it be done? all i can find is info on arrays of stuff ( eg: i want to split this line "hello i'm a test" , so that all i get is "i'm" ) but all i've managed to find is some really complex stuff on msdn about splitting whole lines:~ in vb6 / vb.net i've always been able to do this... Dim text as string text = Split("hello i'm a test",chr(32))(1) /// bringing back the "i'm" bit any ideas?
dynamic wrote: Dim text as string text = Split("hello i'm a test",chr(32))(1) Code this on C# as:
string text = string.Split("hello i'm a test", ' ')[1];
Kant wrote: Actually she replied back to me "You shouldn't fix the bug. You should kill it"
-
dynamic wrote: Dim text as string text = Split("hello i'm a test",chr(32))(1) Code this on C# as:
string text = string.Split("hello i'm a test", ' ')[1];
Kant wrote: Actually she replied back to me "You shouldn't fix the bug. You should kill it"
-
cheers , although i'm struggling with it a bit, if i want to seperate the first space in a row of text, how would it be done? all i can find is info on arrays of stuff ( eg: i want to split this line "hello i'm a test" , so that all i get is "i'm" ) but all i've managed to find is some really complex stuff on msdn about splitting whole lines:~ in vb6 / vb.net i've always been able to do this... Dim text as string text = Split("hello i'm a test",chr(32))(1) /// bringing back the "i'm" bit any ideas?
using System; namespace testString { class testString { public static void Main(string[] args) { string secondword = ""; string text = "hello i'm a test"; string[] fragments = text.Split(new char[] {' '}); if( fragments.Length > 2 ) { secondword = fragments[1]; } Console.WriteLine(text + "\n" + secondword); } } }
This will return i'm. -
using System; namespace testString { class testString { public static void Main(string[] args) { string secondword = ""; string text = "hello i'm a test"; string[] fragments = text.Split(new char[] {' '}); if( fragments.Length > 2 ) { secondword = fragments[1]; } Console.WriteLine(text + "\n" + secondword); } } }
This will return i'm.You can use the code sample I posted above just to test and see it write out the second word to the command line. Alternatively, here is a method you can add to your class or a utility class to just get the second word. This will return the 2nd word, or a zero-length string if there is no 2nd word. Yes I was bored :-) Time to sleep now.
private string GetSecondWord(string TextIn) { string secondword = ""; string[] fragments = TextIn.Split(new char[] {' '}); if( fragments.Length > 2 ) { secondword = fragments[1]; } return secondword; }
Happy To Help, Joe Mozelesky imoz technologies, llc :: powered by imoz