Skip to content

Help! How do you edit this code...

Rukia

Asked Dec 19, 2005, 4:55 PM EST5 answers
Share
LinkedInX
What I am aimed to do is this:

Instead of: I;am;writing;in;C#

I want the output to be:

I
am
writing
in
C#

- Rukia

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Hmm, You can do this much much easier than with this tokenizer class. String class has its own splitter, called Split. It takes char array as a parameter, and it should contain all the delimiters wished to use for splitting (tokenising) the string.

    Here's the example program:

    C#
    using System;
    
    using 
    System.Collections.Generic;
    
    using System.Text;
    namespace TokeniseText
    
    {
    
            
    class 
    Program
    
            {
    
                
        static
    void Main(string[] 
    args)
    
                    
    {
    
                           
    string message =
    "I;am;writing;in;C#";
    
                            
    //split with one delimiter
    
                           
    string[] lines = 
    message.Split(';');
    
    
                           
    foreach (string 
    line in lines)
    
                            
    {
    
                                   
    Console.WriteLine(line);
    
                            
    }
    
    
                           
    Console.WriteLine();
    
                           
    //or
    
    
                           
    string message2 =
    "I;am;writing;in;C#.Isn't.it.nice?;Very.";
    
                           
    //you can add several different delimiters 
    here
    
                           
    //and split the string with all of them at 
    the same time
    
                           
    char[] delimiters =
    new 
    char[] { 
    ';', 
    '.' };
    
                           
    string[] lines2 = 
    message2.Split(delimiters);
    
    
                           
    foreach (string 
    line in lines2)
    
                            
    {
    
                                   
    Console.WriteLine(line);
    
                            
    }
    
                    
    }
    
            }
    
    }

    Result:

    I
    am
    writing
    in
    C#

    I
    am
    writing
    in
    C#
    Isn't
    it
    nice?
    Very

    Press any key to continue . . .

    Edit: single delimiter line simplified

    0
  • 0
    use string.Split(char c) function
    0
  • 0

    >>use string.Split(char c) function

    Yeah, because the method is String.Split(params char[]), you can also give just a char as a parameter.

    0
  • 0
    thanks for the help peoples! my program works now. :)
    0
  • 0
    Or you can use return carrier code , and replace the ";" with the code "\n\r"
    0