Results 1 to 6 of 6

Thread: [RESOLVED] spiliting string into two strings

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    43

    Resolved [RESOLVED] spiliting string into two strings

    Hi

    I want to split a string into two strings

    myString = "hello there are you there test yes I am here"


    I need to split the string into two when it finds test

    eg I want something like spilitString1 ="hello there are you there"
    splitString2 = "yes I am here"

    how do I do this spliting in c#?

    Thanks
    ***learning everyday ***

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: spiliting string into two strings

    See if this helps.

  3. #3
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: spiliting string into two strings

    Here is an example where the splitter is a string in stead of a char array as in Hacks link:


    Code:
                String myString = "hello there are you there test yes I am here";
                String splitter = " test ";
    
    
                String splitString1 = myString.Substring(0, myString.IndexOf(splitter));
                String splitString2 = myString.Substring(myString.IndexOf(splitter) + splitter.Length , myString.Length - (myString.IndexOf(splitter) + splitter.Length ));
    
                Console.WriteLine("String 1: " + splitString1);
                Console.WriteLine("String 2: " + splitString2);


    - ØØ -

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: spiliting string into two strings

    just use Regex.Split
    VB Code:
    1. [COLOR=Blue]string[/COLOR] tmp = "this is a test string";
    2.             [COLOR=Blue]string[/COLOR][] buffer = System.Text.RegularExpressions.Regex.Split(tmp,"test");
    3.              
    4.             Console.WriteLine(buffer[0]); [COLOR=Green]// this is the part before the word ' test ' [/COLOR]
    5.             Console.WriteLine("*** the word test was the split ***");
    6.             Console.WriteLine(buffer[1]); [COLOR=Green]// this is the part after the word ' test '[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: spiliting string into two strings

    Just note that that overload of Regex.Split will split the string on every occurrence of the substring. That may be what you want, but you said you want to split the string in two, so it may be that you only want to split on the first occurrence. If you want to limit the number of substrings in the output you would have to use a different overload, and one that is not Shared I believe.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    43

    Re: spiliting string into two strings

    Thank you all, especialy Noteme thanks for your code snippet. it works alright.

    cheers
    ***learning everyday ***

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