CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  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
    Hanover Germany
    Posts
    20,396

    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
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  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,765

    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
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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
  •  





Click Here to Expand Forum to Full Width

Featured