C#
  Home arrow C# arrow Page 6 - Strings and Characters, Part 2
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Strings and Characters, Part 2
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2004-08-03

    Table of Contents:
  • Strings and Characters, Part 2
  • 2.14 Passing a String to a Method that Accepts Only a Byte[ ]
  • 2.15 Converting Strings to Their Equivalent Value Type
  • 2.16 Formatting Data in Strings Problem
  • 2.17 Creating a Delimited String
  • 2.18 Extracting Items from a Delimited String
  • 2.19 Setting the Maximum Number of Characters a String Can Contain
  • 2.20 Iterating Over Each Character in a String
  • 2.21 Improving String Comparison Performance
  • 2.22 Improving StringBuilder Performance
  • 2.23 Pruning Characters from the Head and/or Tail of a String

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Strings and Characters, Part 2 - 2.18 Extracting Items from a Delimited String


    (Page 6 of 11 )

    Problem

    You have a string, possibly from a text file, which is delimited by one or more characters. You need to retrieve each piece of delimited information as easily as possible.

    Solution

    Using the Split instance method on the string class, we can place the delimited information into an array in as little as a single line of code. For example:

    string delimitedInfo = "100,200,400,3,67";
    string[] discreteInfo = delimitedInfo.Split(new char[1] {','});

    foreach (string Data in discreteInfo)
    Console.WriteLine(Data);

    The string array discreteInfo holds the following values:

    100
    200
    400
    3
    67

    Discussion

    The Split method, like most methods in the string class, is simple to use. This method returns a string array with each element containing one discrete piece of the delimited text split on the delimiting character(s).

    In the Solution, the string delimitedInfo was comma-delimited. However, it could have been delimited by any type of character or even by more than one character. When there is more than one type of delimiter, use code like the following:

    string[] discreteInfo = delimitedInfo.Split(new char[3] {',', ':', ' '});

    This line splits the delimitedInfo string whenever one of the three delimiting characters (comma, colon, or space character) is found.

    The Split method is case-sensitive. To split a string on the letter "a" in a case-insensitive manner, use code like the following:

    string[] discreteInfo = delimitedInfo.Split(new char[1] {'a', 'A'});

    Now, anytime the letter "a" is encountered, no matter what its case, the Split method views that character as a delimiter.

    See Also

    See the “String.Join Method” topic in the MSDN documentation.

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More C# Articles
    More By O'Reilly Media


       · The encoder no longer accepts byte arrays only ubyte arrays so this information is...
     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek