-
February 18th, 2014, 03:24 PM
#1
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???
-
February 18th, 2014, 03:53 PM
#2
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
-
February 18th, 2014, 04:07 PM
#3
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)
-
March 8th, 2014, 02:59 PM
#4
Re: splitting a string into 2 strings c++
-
March 8th, 2014, 07:46 PM
#5
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.
-
March 9th, 2014, 12:46 AM
#6
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.
-
March 9th, 2014, 09:21 AM
#7
Re: splitting a string into 2 strings c++
Originally Posted by laserlight
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|