splitting a string into 2 strings c++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: splitting a string into 2 strings c++

  1. #1
    Join Date
    Jan 2014
    Posts
    12

    splitting a string into 2 strings c++

    hey guys i have an array of strings that i need to split into 2 different string.
    Code:
    string title = "  "Rebecca"  "Alfred Hitchcock" ";
    
    so far all the data is in one string called title
    //need to break them into 2 strings, title and director
    // title should contain = Rebecca
    // director should contain alfred hitchcock
    please help, which string fuctions can i use to split these 2 and remove the quotes???

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,680

    Re: splitting a string into 2 strings c++

    First your question has nothing to do with the Visual C++ Programming, So I will now move this thread to the appropriate forum.
    Second, since you are trying to use std::string why don't you want to look at the std::string methods?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    4,755

    Re: splitting a string into 2 strings c++

    Code:
    string title = "  "Rebecca"  "Alfred Hitchcock" ";
    This is not a valid string. If you want " inside of " then you need to escape the quote or alternatively have 'Rebecca' etc rather than "Rebecca".

    Either
    Code:
    string title = "  \"Rebecca\"  \"Alfred Hitchcock\" ";
    string title = "  'Rebecca'  'Alfred Hitchcock' ";
    Is this a homework assignment?

    Hint: Look at find() and substr() string methods.
    Last edited by 2kaud; February 18th, 2014 at 05:13 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2015

  4. #4
    Join Date
    Mar 2014
    Location
    Warsaw, Poland
    Posts
    3

    Re: splitting a string into 2 strings c++


  5. #5
    Join Date
    Jul 2013
    Posts
    576

    Re: splitting a string into 2 strings c++

    The simplest and often forgotten option is to just scan the string char by char and do what ever you like with it.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,705

    Re: splitting a string into 2 strings c++

    There's a level of abstraction available here with find and substr that I think would be better than a lower level scanning of the string char by char to find those quotes and the substrings between them.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    4,755

    Re: splitting a string into 2 strings c++

    Quote Originally Posted by laserlight View Post
    There's a level of abstraction available here with find and substr that I think would be better than a lower level scanning of the string char by char to find those quotes and the substrings between them.
    For the OP requirement, I agree.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2015

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center