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 topsyZDNet is available in the following editions:
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.
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:
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>
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 topsyit 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 enterprisesit 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 enterprisesi would look into technical colleges around ur area to see if they offer that program. most technical schools offer it.
Joliese Tan
@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 topsyMB 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 topsyAs 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 topsyRT @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 topsyRead my blog post on getting the most from your Nexus One: http://www.zdnetasia.com/blogs/m...
16 hours 14 minutes ago by mistertechblog on twitterRT @3wconsulting: Whitepaper from http://3W.com.au "Outsourcing Your IT Requirements to Philippines" now on @zdnetaustralia & @zdnetasia http://ow.ly/1oY9f
1 day 28 minutes ago by LeesaAT3W on twitterWhitepaper from http://3W.com.au "Outsourcing Your IT Requirements to Philippines" now on @zdnetaustralia & @zdnetasia http://ow.ly/1oYbA
1 day 29 minutes ago by itemployment on twitterWhitepaper from http://3W.com.au "Outsourcing Your IT Requirements to Philippines" now on @zdnetaustralia & @zdnetasia http://ow.ly/1oYbz
1 day 29 minutes ago by brucemills on twitterZdnetasia.com Estimated Worth $178,365 USD. Daily Ad Revenue:$244 USD, Daily Views:81,445 Pages... - http://www.haplog.com/www.zdneta...
1 day 12 minutes ago by Haplog on twitterThe 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 lifeThanks 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 drivesIt 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 AsiaCOL 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 PCsHigh performance computing (HPC) most-wanted job in Asia http://bit.ly/9vFC3i (via @zdnetasia) #singapore
2 days 46 minutes ago by mySingapore on twitterRT @zdnetasia: EMC COO, Pat Gelsinger, on bridging gaps in the organization and its cloud ambitions in Asia. (cont) http://tl.gd/i5jjd
2 days 35 minutes ago by mistymaitimoe on twitterEMC COO, Pat Gelsinger, on bridging gaps in the organization and its cloud ambitions in Asia. http://bit.ly/9etOZW
2 days 39 minutes ago by zdnetasia on twitterAsian SMBs need to pay more attention to disaster recovery planning http://bit.ly/bDet08 via @zdnetasia
2 days 54 minutes ago by asiapacsolution on twitterAsian SMBs need to pay more attention to disaster recovery planning http://bit.ly/bDet08
2 days 9 minutes ago by zdnetasia on twitter[TECH] URL Shorteners slow Web redirection. - http://bit.ly/bySnWK @zdnetasia
3 days 52 minutes ago by danielcktan on twitterURL shorteners are great but they can slow web redirection & you pray it would never go down http://bit.ly/bySnWK via @zdnetasia
3 days 20 minutes ago by angahsin on twitterURL shorteners slow Web redirection. http://bit.ly/bySnWK
3 days 49 minutes ago by zdnetasia on twitterChinese agencies cry foul over Google. http://bit.ly/by6rwV
3 days 55 minutes ago by zdnetasia on twitterall 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 businessit is not to good for china.
Proactol
Very good explanation of JMX
4 days 3 minutes ago by Babith B on Managing applications with JMXThe 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 diggsAnother ZTE story....
5 days 9 minutes ago by Moderate Your Greed on Philippines opens bid for final 3G licenseWe 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 increaseI 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 insteadThe Desktop Virtualization Revolution is here!
Find our more with Citrix Simplicity is Power
2010 IT Salary & Skills Report
Find out the salary range of IT professionals. Join activeTechPros for free access to the report.
The Internet Show 2010, 21-22 Apr 2010, Singapore
FREE admission for visitors who pre-register online. Register Today!
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