We have lots of string handling functions in C#.net. We are just going to see some basic
functions.
C# string Split() - Splitting a string to an Array
The split() can be used to Split an entire string in to an string array. This will be
done using this split method. This can be used when you need long strings to be splitted
to seperate string objects.
---------------------------------------------
string mString = "This is to show Splitting in strings";
string[] mArr = mString.Split(' ');
---------------------------------------------
Here in the above code we have a string named mString and we have assigned a string to
it. Now check the second line of it. We have created an String Array to store the values
that gets Splitted.
mString.Split(' ') method splits the Entire string into an array. We have given a white
space as the argument for the Split method for the string. This will allow it to split
the string whenever it sees a white space inside the string.
Now, if you need to get the Splitted values of the string you must have to fetch the
entire items of the Array mArr using some loops.
C# string.Join() - Joining an Array as a String
The Join() method can be used to join an Array to a single String. If we have an array
to be used as a single string you can use this string.Join() method.
Let's see an example for it
---------------------------------------------
string joinedString = string.Join("-", mArr);
//Here the JoinedString variables value will be
// "This-is-to-show-Splitting-in-strings"
---------------------------------------------
In the above code in the Join function we have two overloads mainly
string.Join("String Separator","The array to Join");
Here in the above we have set the Separator as "-"(hyphen). So it will attach each Array
element with this "-". Like wise we can join the Strings in any way we like.
No comments:
Post a Comment