Geeks With Blogs

@RiaGuru
  • RiaGuru Attending windows azure spring camp @Chennai and its cool about 854 days ago
  • RiaGuru I hosted my blog http://t.co/G1ZHfZB Its related to asp.net mvc,nhibernate. The blog is build on top of funnelweblog.com framework. about 1194 days ago
  • RiaGuru I recently experienced the power of NHIbernate. A very powerful ORM ! about 1312 days ago

Thanigainathan Siranjeevi Sharing my learning

Introduction:

.Net 3.5 has some interesting string functions. I almost admire their usage. Following are those functions that we will be discussing here.

  1. String.IsNullOrEmpty()
  2. String.Split()(Available from .Net 1.0)
  3. 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# 

{
    string strSplt = null;
    strSplt = "George,Clooney,James,Bond";
}
I can use the Split function here to read the string values one by one as follows.
VB.Net 
        Dim strSplt As String
        strSplt = "George,Clooney,James,Bond"
 
        For Each st As String In strSplt.Split(",")
            Response.Write(st)
            Response.Write("<br \>")
        Next
C#
        Dim strSplt As String
        strSplt = "George,Clooney,James,Bond"
 
        For Each st As String In strSplt.Split(",")
            Response.Write(st)
            Response.Write("<br \>")
        Next
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 
  1. System.ArgumentOutOfRangeException: count is negative.
  2. 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.

Posted on Saturday, May 9, 2009 6:05 PM | Back to top


Comments on this post: Interesting .Net String Functions

# re: Interesting String Functions - .Net 3.5
Requesting Gravatar...
Your IsNullOrEmpty function is not correct in C#. Should be:

{
string tstStr = null;
if (string.IsNullOrEmpty(tstStr)) {
Response.Write("String is empty or null");
}

Looks like the Copy/Paste monster gotcha!

~Lee
Left by Lee Brandt on May 09, 2009 8:57 PM

# re: Interesting String Functions - .Net 3.5
Requesting Gravatar...
This site doesn't work in Firefox ;)
I follow your links of three String's methods and see that they were available in .net 1.0 (only IsNullOrEmpty in 2.0) ?
Left by Duy Lam on May 10, 2009 3:55 AM

# re: Interesting String Functions - .Net 3.5
Requesting Gravatar...
Hi Duy Lam,

Thanks for the reply. As you said those two functions are from 1.0 itself. I just want to share the interesting features of this method's.

Thanks,
Thani
Left by Thanigainathan Siranjeevi on May 10, 2009 5:57 AM

# re: Interesting String Functions - .Net 3.5
Requesting Gravatar...
Hi Lee,

I have updated the code now. Thanks for pointing that. Also the theme is changed now and can be viewed in Firefox also.

Thanks,
Thani
Left by Thanigainathan Siranjeevi on May 10, 2009 6:07 AM

Your comment:
 (will show your gravatar)
 


Copyright © thanigai | Powered by: GeeksWithBlogs.net | Join free