cutting up strings

This is a discussion on cutting up strings within the C++ Programming forums, part of the General Programming Boards category; hi, im wondering how to go about cutting up strings into smaller strings based on keywords. For example i have ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    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


    \device\lrbtgureutghilutrgiltg\device\bvgliuerbgio ubtwbrtibwg\device\vblerobrviobvu


    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

    thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    find() + substr() + a loop - what's the problem?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    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

    Code:
                    string subString = "";
                    string cutUp = "\\DEVICE";
                    size_t found;
                    size_t found2;
    
                    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);
    }
    I think im missing a few parts of the process as im not achieving the correct string split

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    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?

    thanks


    Code:
                    string str2 ("\\DEVICE");
                    size_t found;
                    size_t found2;
    
    
    
                   found=targetString.find(str2);
                   if (found!=string::npos)
    
    
                   found2=targetString.find("\\DEVICE",found+1);
                   if (found!=string::npos)
    
                   path = str.substr (found,found2);

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    You may consider using a Perfect C++ String Explode or Split technique to make it a lot easier and OOP.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    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?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by pastitprogram View Post
    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.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    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.


    Code:
         int lengthOfString = str.length();   //getting the length of big string
    
                    string str2 ("\\DEVICE");       // delimiter to cut up string
                    size_t found;
                    size_t found2;
                    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
                   if (found!=string::npos)
    
    
                   found2=str.find("\\DEVICE",found+1);           //finding second instance
                   if (found!=string::npos)
    
                   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
    
                     }

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's way more complicated than what you had before.
    Code:
    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
    end while

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21