Splitting string in chunks of 2 characters

This is a discussion on Splitting string in chunks of 2 characters within the C++ Programming forums, part of the General Programming Boards category; I have a string of type std::string, and I want to split it so that 210100 would become 21 01 ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22

    Splitting string in chunks of 2 characters

    I have a string of type std::string, and I want to split it so that 210100 would become 21 01 00 in an array of strings of type:

    Code:
    std::string name[] = {"21","01","00"};
    However, I have no idea of how to do this in C++

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Code:
    void split(const std::string str, std::vector<std::string>& out)
    {
        out.clear();
        out.reserve(str.length() / 2 + str.length() % 1);
        for (size_t i = 0; i < str.length(); i += 2)
        {
            out.push_back(std::string(str, i, 2));
        }
    }
    I never put signature, but I decided to make an exception.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22
    Quote Originally Posted by kmdv View Post
    Code:
    void split(const std::string str, std::vector<std::string>& out)
    {
        out.clear();
        out.reserve(str.length() / 2 + str.length() % 1);
        for (size_t i = 0; i < str.length(); i += 2)
        {
            out.push_back(std::string(str, i, 2));
        }
    }
    Thanks much, I have a few questions though.

    This function works with two arguments, the original string and the final string (result). It seems to be like the out string is of type vector, but what it is. I also want to know if there's any way I can make it a std::string.

    Thanks much

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    You want to have an array of strings, vector is one.
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    It should be:
    Code:
    void split(const std::string& str, std::vector<std::string>& out)
    I never put signature, but I decided to make an exception.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22
    Is there a way to make a vector an array of strings?

    Thanks

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    vector IS an array of string
    I never put signature, but I decided to make an exception.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22
    Code:
    std::string greetings[] = {"hey","hi","hello"};
    Is this a vector?

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    This is NOT a vector. This is an array, but it's something different. This is a built-in array type and it has fixed size. Vector is an array too, but it's a dynamic one, which can grow and shrink in size. You could split your string to this kind of array too, but you will have to set limit for length.
    I never put signature, but I decided to make an exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Superfluous characters after string -- pointer issues?
    By marshaul in forum C Programming
    Replies: 21
    Last Post: 01-04-2011, 03:13 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

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