We have relaunched: What's new at ZDNet Asia?

Easily parse string values with .NET

Summary

The .NET Framework simplifies processing and formatting data with the String class and its Split and Join methods or regular expressions. Learn more about using these methods in your application.

Events

Microsoft MSDN/Developer Event
25 Mar 2010

One Marina Boulevard, Microsoft Singapore

IT Architect Regional Conference Singapore 2010
20 - 21 Apr 2010

Singapore Management University, Singapore

The Internet Show 2010
21-22 Apr 2010

Suntec Singapore

Processing string values is an integral aspect of most application development projects. This often involves parsing strings into separate values. For instance, receiving data from an external data source such as a spreadsheet often utilizes a common format like comma-separated values. The .NET String class simplifies the process of extracting the individual values between the commas.

Extracting values
The Split method of the String class allows you to extract individual values separated by a specific character. The separator value is passed to the method, which is overloaded with its second variation accepting a second parameter that specifies the maximum number of elements to return (extract from the string value). (Note: You can specify more than one separator in a character array.) The values pulled from the string are returned in a String array.

Here are the two variables:

  • String.Split(char[]) in C# or String.Split(Char()) in VB.NET
  • String.Split(char[], int) in C# or String.Split(Char(), Integer) in VB.NET

The following C# snippet populates an array with values contained in a comma-separated string value:

string values = "TechRepublic.com, CNET.com, News.com, Builder.com, GameSpot.com";

string[] sites = values.Split(',');
foreach (string s in sites) {
Console.WriteLine(s);

}

The following output is generated:

TechRepublic.com

CNET.com

News.com

Builder.com

GameSpot.com

The equivalent VB.NET code follows:

Dim values As String

values = "TechRepublic.com, CNET.com, News.com, Builder.com, GameSpot.com"

Dim sites As String() = Nothing

sites = values.Split(",")

Dim s As String

For Each s In sites
Console.WriteLine(s)

Next s

You may specify multiple separator characters, which are contained in a character array. The following code splits a string of values separated by a comma, semicolon, or colon. In addition, it uses the optional second parameter to set the maximum number of items returned at four.

char[] sep = new char[3];

sep[0] = ',';

sep[1] = ':';

sep[2] = ';';

string values = "TechRepublic.com: CNET.com, News.com, Builder.com; GameSpot.com";

string[] sites = values.Split(sep, 4);
foreach (string s in sites) {
Console.WriteLine(s);

}

The following output is generated (notice that the second parameter places the remainder of the string in the last array element):

TechRepublic.com

CNET.com

News.com

Builder.com; GameSpot.com

The equivalent VB.NET code follows:

Dim values As String

values = "TechRepublic.com: CNET.com, News.com, Builder.com; GameSpot.com"

Dim sites As String() = Nothing

Dim sep(3) As Char

sep(0) = ","

sep(1) = ":"

sep(2) = ";"

sites = values.Split(sep, 4)

Dim s As String

For Each s In sites
Console.WriteLine(s)

Next s

While the Split method allows you to easily work with individual elements contained in a string value, you may need to format values according to a predefined format like comma-separated values. The String class makes it easy to assemble a properly formatted string.

Putting it together
The Join method of the String class accepts the character to be used as the separator as its first parameter. The values to be concatenated are passed as the second parameter in the form of a string array. It has one overloaded method signature that accepts integer values as the third and fourth parameters. The third parameter specifies the first array element to use, and the last parameter is the total number of elements to use.

The following C# code sample demonstrates assembling the values used in the previous example:

string sep = ", ";

string[] values = new String[5];

values[0] = "TechRepublic.com";

values[1] = "CNET.com";

values[2] = "News.com";

values[3] = "Builder.com";

values[4] = "GameSpot.com";

string sites = String.Join(sep, values);
Console.Write(sites);

The following output is generated:

TechRepublic.com, CNET.com, News.com, Builder.com, GameSpot.com

The equivalent VB.NET follows:

Dim sep As String

sep = ", "

Dim values(4) As String

values(0) = "TechRepublic.com"

values(1) = "CNET.com"

values(2) = "News.com"

values(3) = "Builder.com"

values(4) = "GameSpot.com"

Dim sites As String

sites = String.Join(sep, values)
Console.Write(sites)

We could use the overloaded format to specify where to begin and how many elements to include in the result. The following sample begins with the second (note that element numbering begins at zero) and returns a maximum of three elements:

Dim sep As String

sep = ", "

Dim values(4) As String

values(0) = "TechRepublic.com"

values(1) = "CNET.com"

values(2) = "News.com"

values(3) = "Builder.com"

values(4) = "GameSpot.com"

Dim sites As String

sites = String.Join(sep, values, 2, 3)
Console.Write(sites)

The starting element number and the maximum values to return must be valid within the string array being used. If either is invalid (i.e., not contained in the array), then an exception is thrown. For this reason, it is a good idea to utilize a try/catch block to handle any problems.

While the String class provides the necessary methods, it isn't the only way to handle the parsing of a string value. Another common approach takes advantage of regular expressions.

Parsing with regular expressions
The .NET Framework provides the Regex class contained in the System.Text.RegularExpressions namespace for using regular expressions within a .NET application. Parsing is only one of the many applications of regular expressions.

Let's examine the parsing of our sample string using regular expressions. The following ASP.NET page uses C# to parse a comma-delimited list of sites into an array:

<%@ Page Language="C#" Debug="true" %>

<%@ Import Namespace="System.Text.RegularExpressions" %>

<script language="C#" runat="server">

private void Page_Load(object sender, System.EventArgs e){

if (!IsPostBack) {

string values = "TechRepublic.com, CNET.com, News.com, Builder.com, GameSpot.com";

string pattern = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
Regex r = new Regex(pattern);

string[] sites = r.Split(values);
foreach (string s in sites) {
Response.Write(s);
Response.Write("<br>");

} } }

</script>

The equivalent VB.NET code follows. Notice that the inclusion of quotation marks in the string value (pattern) causes problems. So, the quotation marks contained in the string must be escaped to be recognized; this may be achieved by placing two of the characters adjacent to each other.

<%@ Page Language="VB" Debug="true" %>

<%@ Import Namespace="System.Text.RegularExpressions" %>

<script language="VB" runat="server">

Sub Page_Load

If Not (IsPostBack) Then

Dim values As String

values = "TechRepublic.com, CNET.com, News.com, Builder.com, GameSpot.com"

Dim pattern As String

pattern = ",(?=(?:[^\""]*\""[^\""]*\"")*(?![^\""]*\\""))"

Dim r As Regex

r = new Regex(pattern)

Dim sites As String()

sites = r.Split(values)

Dim s As String

For Each s In sites
Response.Write(s)
Response.Write("<br>")

Next s

End If

End Sub

</script>

Talkback

Easily parse string values with .NET

Dim values As String

values = "TechRepublic.com, CNET.com, News.com, Builder.com, GameSpot.com"

Dim sites As String() = Nothing

sites = values.Split(",")


does not work

Ingmar Eidem April 24th, 2009 Reply
Add your opinion

In order to post a comment, you need to be registered. (Sign In or register below)

Post your comment
Transform your business interactions with real-time voice, video and telepresence solutions.
Tech Vendor: Cisco

ZDNet Asia Live

Reasons to care about Viacom v. Google - Zd Net Asia.com: Last Thursday's 200-page dump of cour... http://bit.ly/crqRzF #SME #UMG #WMG #EMI

31 minutes ago by metaphysicalist on topsy

it depend of his culture the modern ones yes , but the old fashion no , if he like you for not serious relationship , the he just need a ...

1 hour 29 minutes ago by collingridge on Philippine antipiracy drive focuses on enterprises

it depend of his culture the modern ones yes , but the old fashion no , if he like you for not serious relationship , the he just need a ...

1 hour 29 minutes ago by collingridge on Philippine antipiracy drive focuses on enterprises

i would look into technical colleges around ur area to see if they offer that program. most technical schools offer it.
Joliese Tan

1 hour 32 minutes ago by collingridge on EMC ramps up cloud efforts through tighter alliances

@BarackObama People voted you in for change. Why are you not listening on ACTA http://tinyurl.com/y8u56g9 #hcr HCR

1 hour 42 minutes ago by studio1411 on topsy

MB Kabbalah IChing - Free Software Downloads - ZDNet Asia: MB Kabbalah IChing is a zodiac sign based software that... http://bit.ly/czUQRr

2 hours 36 minutes ago by fighting_jew on topsy

As Sony camera users, both MTS and M2TS are Sony high definition video file types, which are raw AVCHD videos recorded by AVCHD camcorder...

3 hours 18 minutes ago by tracyjump on Mobile data centers becoming 'mainstream'

Found this great little deal calculator http://www.zdnetasia.com/downloa...

9 hours 57 minutes ago by winstoncranford on topsy

RT @mistertechblog: I wrote about Nexus One and Touchdown, desktop dock, Bluetooth/USB tethering, ebooks here: http://bit.ly/bRdzx0

16 hours 8 minutes ago by yklee13 on topsy

Read my blog post on getting the most from your Nexus One: http://www.zdnetasia.com/blogs/m...

RT @3wconsulting: Whitepaper from http://3W.com.au "Outsourcing Your IT Requirements to Philippines" now on @zdnetaustralia & @zdnetasia http://ow.ly/1oY9f

Whitepaper from http://3W.com.au "Outsourcing Your IT Requirements to Philippines" now on @zdnetaustralia & @zdnetasia http://ow.ly/1oYbA

Whitepaper from http://3W.com.au "Outsourcing Your IT Requirements to Philippines" now on @zdnetaustralia & @zdnetasia http://ow.ly/1oYbz

Zdnetasia.com Estimated Worth $178,365 USD. Daily Ad Revenue:$244 USD, Daily Views:81,445 Pages... - http://www.haplog.com/www.zdneta...

The receivers don't transmit back to the satellite. Unless there is a phone line attached to the receiver, they don't have any wa...

2 days 54 minutes ago by bessellbrowne on Apple to join the geolocation craze?

whatever little understanding I have we 'll only progress toward end of the world if we use HPCs to lenthen life of human being. Huma...

2 days 1 minute ago by abhi32002@gmail.com on High computing promises elixir of life

Thanks for the knowledgeable article on SDDs. Allas...when all this reasearch will happen in Indian Universities. Hope the new bill on Fo...

2 days 13 minutes ago by abhi32002@gmail.com on APAC HPC users eye solid-state drives

It was a good article. This brings a good opportunity for Indian IT firms to come up with new solutions in this field. HPC can become a b...

2 days 32 minutes ago by abhi32002@gmail.com on High computing most-wanted job in Asia

COL KR DHARMADHIKARY(RETD) its very late to reply the link, but if it is still alive and looking for opportunity, i would like to know th...

2 days 29 minutes ago by deb021280 on Education takes off in rural India, helped by PCs

High performance computing (HPC) most-wanted job in Asia http://bit.ly/9vFC3i (via @zdnetasia) #singapore

RT @zdnetasia: EMC COO, Pat Gelsinger, on bridging gaps in the organization and its cloud ambitions in Asia. (cont) http://tl.gd/i5jjd

EMC COO, Pat Gelsinger, on bridging gaps in the organization and its cloud ambitions in Asia. http://bit.ly/9etOZW

Asian SMBs need to pay more attention to disaster recovery planning http://bit.ly/bDet08 via @zdnetasia

Asian SMBs need to pay more attention to disaster recovery planning http://bit.ly/bDet08

[TECH] URL Shorteners slow Web redirection. - http://bit.ly/bySnWK @zdnetasia

URL shorteners are great but they can slow web redirection & you pray it would never go down http://bit.ly/bySnWK via @zdnetasia

URL shorteners slow Web redirection. http://bit.ly/bySnWK

Chinese agencies cry foul over Google. http://bit.ly/by6rwV

all of sg's isps have been practising compulsory invisible proxy for all home subscribers at their backend since many years back alre...

3 days 13 minutes ago by melvinchia on Web filters mean bad news for business

it is not to good for china.
Proactol

4 days 58 minutes ago by nathonastle on Chinese ad partners beg Google for information

Very good explanation of JMX

4 days 3 minutes ago by Babith B on Managing applications with JMX

The reaction to a report issued Tuesday by Flurry Analytics managed to completely overlook some interesting news--the Android-based Motorola Droid outsold the original iPhone over the same period of time following their respective launches--to focus instead on the sales numbers for the Nexus One.

5 days 7 minutes ago by lonemavericks on diggs

Another ZTE story....

5 days 9 minutes ago by Moderate Your Greed on Philippines opens bid for final 3G license

We at www.fifosys.com have also seen a growth in IT outsourcing and anticipate it as a growing field.

5 days 42 minutes ago by sarah Jane on Companies' outsourcing spend to increase

I agree with you. The iSiVaL is super portable and TVs can't expand their image size. I recorded a video that might bring some ideas to...

5 days 12 minutes ago by Jesse B Andersen on Buying a projector? Try an LED TV instead