Introduction:
.Net 3.5 has some interesting string functions. I almost admire their usage. Following are those functions that we will be discussing here.
- String.IsNullOrEmpty()
- String.Split()(Available from .Net 1.0)
- String.Join()(Available from .Net 1.0)
Reference Links
String.IsNullOrEmpty - http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx
String.Split() -http://msdn.microsoft.com/en-us/library/system.string.split.aspx
String.Join()-http://msdn.microsoft.com/en-us/library/system.string.join.aspx
Target Audience:
The target audience should have knowledge in VB.Net or C#.Net to get a clear understanding of these functions.
String.IsNullOrEmpty()
This is the function to test a string for either Null reference or Emptiness. This is something equal to
VB.Net
Dim tstStr As String
If tstStr = String.Empty Or tstStr = Nothing Then
Response.Write("String is empty or null")
End If
C#.Net
{
string tstStr = null;
if (tstStr == string.Empty | tstStr == null) {
Response.Write("String is empty or null");
}
}
Which can be written with this function as
VB.Net
If String.IsNullOrEmpty(tstStr) Then
Response.Write("String is empty or null")
End If
C#.Net
{
string tstStr = null;
if (String.IsNullOrEmpty(tstStr)) {
Response.Write("String is empty or null");
}
}
This can be very useful in practical scenarios when it avoids the number of lines of code. Also performance gain will be good when using this functions.
String.Split()
I have a string which has comma separated values and want to fetch the values one by one from them. Here comes the rescue through the function Split(). Consider for example i have a String as follow’s
VB.Net
Dim strSplt As String
strSplt = "George,Clooney,James,Bond"
C#
strSplt = "George,Clooney,James,Bond";
I can use the Split function here to read the string values one by one as follows.
Dim strSplt As String
strSplt = "George,Clooney,James,Bond"
For Each st As String In strSplt.Split(",")
Response.Write(st)
Response.Write("<br \>")
Next
strSplt = "George,Clooney,James,Bond"
For Each st As String In strSplt.Split(",")
There are various optional parameters available with this functions. I have listed them below.
| Function |
Description |
| Split(ByVal Paramarray separator() char) as String() |
Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.
|
| Public Function Split(ByVal separator() As Char, ByVal count As Integer) As String() |
Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array. A parameter specifies the maximum number of substrings to return.
|
| Public Function Split(ByVal separator() As Char, ByVal count As Integer, ByVal options As System.StringSplitOptions) As String() |
Returns a string array that contains the substrings in this string that are delimited by elements of a specified Unicode character array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.
|
| Public Function Split(ByVal separator() As Char, ByVal options As System.StringSplitOptions) As String() |
Returns a string array that contains the substrings in this string that are delimited by elements of a specified Unicode character array. A parameter specifies whether to return empty array elements.
|
| Public Function Split(ByVal separator() As String, ByVal count As Integer, ByVal options As System.StringSplitOptions) As String() |
Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.
|
| Public Function Split(ByVal separator() As String, ByVal options As System.StringSplitOptions) As String() |
Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. A parameter specifies whether to return empty array elements.
|
There are two types of exceptions available for this fuction. They are
- System.ArgumentOutOfRangeException: count is negative.
- System.ArgumentException: options is not one of the System.StringSplitOptions values
The Split() function is widely used for the ListBox controls. Particularly when there are user controls which produce the selected values as comma separated values .
String.Join()
This function is just the reverse of String.Split(). The responsibility of this function is to concatenate string values with a separator value. The separator is another character like “,” or “;”. The input for this method will be a String array. If there is string array .Then we can use the following way to join the items .
VB.Net
Dim strJn(5) As String
strJn(0) = "Sample"
strJn(1) = "Test"
strJn(3) = "For"
strJn(4) = "Joins"
Response.Write(String.Join(",", strJn))
C#
{
string[] strJn = new string[6];
strJn(0) = "Sample";
strJn(1) = "Test";
strJn(3) = "For";
Response.Write(string.Join(",", strJn));
}
Following are the two options to use this function.
| Name |
Description |
| Public Shared Function Join(ByVal separator As String, ByVal value() As String) As String |
Concatenates a specified separator System.String between each element of a specified System.String array, yielding a single concatenated string. |
|
Public Shared Function Join(ByVal separator As String, ByVal value() As String, ByVal startIndex As Integer, ByVal count As Integer) As String
|
Concatenates a specified separator System.String between each element of a specified System.String array, yielding a single concatenated string. Parameters specify the first array element and number of elements to use. |
The exception’s that are available with this method’s are
1.System.ArgumentNullException: value is null.
2.System.ArgumentOutOfRangeException: startIndex or count is less than 0. -or- startIndex plus count is greater than the number of elements in value.
3.System.OutOfMemoryException: Out of memory.
This is something equal to Sql Server’s Coelesce which can be used to join the string values in Sql Server.
Conclusion:
There are a lot of function’s available with .Net. Of these i have covered these three functions as they found usage in practical scenario’s. Please let me know your feedback on this.