Results 1 to 7 of 7

Thread: [RESOLVED] space as delimeter for string split

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Resolved [RESOLVED] space as delimeter for string split

    I'm trying to split a string based on a space.

    How do I specify a space as the delimeter.

    My sample code :

    Code:
    string s = "sss ttt";
    string delimeter =@" ";
                System.Text.RegularExpressions.Regex.Split(s, delimeter);

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: space as delimeter for string split

    A space does not constitute a regular expression. Just use normal String.Split.

    Code:
    string s = "sss ttt";
    string words = s.Split(' ');

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Re: space as delimeter for string split

    Well i tried it but i get an error as follows

    Error 1 Cannot implicitly convert type 'string[]' to 'string'

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: space as delimeter for string split

    I knew I would do that it's meant to be string[] words.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Re: space as delimeter for string split

    ya thanks alot ....one more problem.

    I want to get a part of a string from the right-side of it.

    Say for example string "abcdefgh".
    I want to be able to return the last 3 characters fgh.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: space as delimeter for string split

    Use Substring:

    Code:
    string whole_thing = "abcdefgh";
    
    string just_a_bit = whole_thing.Substring(whole_thing.Length - 3, 3);

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Re: space as delimeter for string split

    Hi,

    Thanks so much. It worked fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width