cutting up strings
hi, im wondering how to go about cutting up strings into smaller strings based on keywords. For example i have the following string
At the moment it is all stored in one string however I want to break it down so that it becomes
string1 = \device\lrbtgureutghilutrgiltg
string2 = \device\bvgliuerbgioubtwbrtibwg
string3 = \device\vblerobrviobvu
Each string begins with "\device" and ends with "\device" as it could just be random input up until the next "\device" entry in the string.
Is there a way to spilt this string up? I thought it mite get more complex as the string ends and begins with the same criteria so i thought this may cause some problems. Any pointers would be appreciated
find() + substr() + a loop - what's the problem?
Or perhaps you could use the iterator version of the constructor, which starts at the first iterator and goes up to, but not including, the second iterator. (Of course you'd get the iterators by calling find.)
thank you for your replies, ive had a go at the following but i think im struggling a bit with putting it into a loop until the entire string is cut up
I think im missing a few parts of the process as im not achieving the correct string split
string subString = "";
string cutUp = "\\DEVICE";
found=targetstring.find(cutUp); // targetstring is the string like in my first post that needs to be cut up
found = found+1;
while ( found2 != targetstring.find(cutUp)
subString = targetString.substr (found2);
So substr takes two arguments, the second being the length of the substring you want, so you should maybe use two arguments. found+1 will remove the backslash in front of device if that's what you want.
thanks, ive had a bit test with some changes and now I have the code below. Ive magae to get it to strip out the very first part of my string but it only does the first part so im guessing this is where a loop plays its role. Im not too sure how i would impliment it in this situation. Is there and equvilent EOF clause for string, as in do something until end of string? or would I have to delete the first bit from my overal string?
string str2 ("\\DEVICE");
path = str.substr (found,found2);
You may consider using a Perfect C++ String Explode or Split technique to make it a lot easier and OOP.
thanks for your advice, but is there no way to impliment a loop for the code i already have that will run through my string and split it up?
Of course there is. Since you've basically already got it written, there you go. The only thing to keep in mind is that you only need to do one extra find per loop, instead of two, since the "stop here" position becomes the "start here" position for the next one.
Originally Posted by pastitprogram
thats where im getting things wrong wen it comes to working my way through the loop. Im not sure im implimenting my loop correctly as it keeps infinatley running. I have decided to get the overall string size then erase each part from the loop size as i find it but im thinking this is a rubbish way of doing it.
int lengthOfString = str.length(); //getting the length of big string
string str2 ("\\DEVICE"); // delimiter to cut up string
string path = ""; // string to recieve each slic of string
while (lengthOfString > 1) //looping through length of string until empty
found=str.find(str2); // finding first instance
found2=str.find("\\DEVICE",found+1); //finding second instance
path = str.substr (found,found2); //putting in string
str.erase (0,found2); // erasing these characters from overall string
lengthOfString = lengthOfString - found2; //minusing length of string from loop
That's way more complicated than what you had before.
Find the first one
Find the second one
while (second one != npos)
first one += 1 (to get rid of the slash)
target = substr(first one, second one)
first one = second one
second one = find again, starting from second one+1