Splitting a String with 2 input variables, "beginstr" and "endstr"

Discussion in 'C++' started by kevineller794@gmail.com, Sep 2, 2008.

  1. Guest

    I want to make a split string function, but it's getting complicated.

    What I want to do is make a function with a String, BeginStr and an
    EndStr variable, and I want it to return it in a char array.

    For example:

    char teststr[255];
    strcpy(teststr, "test:blah;");

    char returned_string[255];
    strcpy(returned_string, splitstring(teststr, "test:", ";"));

    and it returned_string would = blah

    can someone help me?

    Thank you, help is much appreciated =)
    , Sep 2, 2008
    #1
    1. Advertising

  2. On 2008-09-02 19:31, wrote:
    > I want to make a split string function, but it's getting complicated.
    >
    > What I want to do is make a function with a String, BeginStr and an
    > EndStr variable, and I want it to return it in a char array.
    >
    > For example:
    >
    > char teststr[255];
    > strcpy(teststr, "test:blah;");
    >
    > char returned_string[255];
    > strcpy(returned_string, splitstring(teststr, "test:", ";"));
    >
    > and it returned_string would = blah
    >
    > can someone help me?


    Sure, first dump the char arrays and use std::string. Then read up on
    std::string::find() and std::string::substr(). Start by finding BeginStr
    and get a substring of everything after it, then find EndStr in that and
    return everything in front of it.

    --
    Erik Wikström
    Erik Wikström, Sep 2, 2008
    #2
    1. Advertising

  3. Guest

    Re: Splitting a String with 2 input variables, "beginstr" and"endstr"

    On Sep 2, 2:07 pm, Erik Wikström <> wrote:
    > On 2008-09-02 19:31, wrote:
    >
    >
    >
    > > I want to make a split string function, but it's getting complicated.

    >
    > > What I want to do is make a function with a String, BeginStr and an
    > > EndStr variable, and I want it to return it in a char array.

    >
    > > For example:

    >
    > > char teststr[255];
    > > strcpy(teststr, "test:blah;");

    >
    > > char returned_string[255];
    > > strcpy(returned_string, splitstring(teststr, "test:", ";"));

    >
    > > and it returned_string would = blah

    >
    > > can someone help me?

    >
    > Sure, first dump the char arrays and use std::string. Then read up on
    > std::string::find() and std::string::substr(). Start by finding BeginStr
    > and get a substring of everything after it, then find EndStr in that and
    > return everything in front of it.
    >
    > --
    > Erik Wikström


    Ok I did that, but my string here is:

    "test1=test1; test2=test2;"

    and it finds test2, but it only finds the FIRST ";", so it's saying
    that the EndStr is before the BeginStr, and it doesn't do this
    correctly,
    any remedy?

    I'm sorry I didn't include that I needed to parse a whole line of
    these from a text file.
    , Sep 2, 2008
    #3
  4. LR Guest

    wrote:
    > I want to make a split string function, but it's getting complicated.
    >
    > What I want to do is make a function with a String, BeginStr and an
    > EndStr variable, and I want it to return it in a char array.
    >
    > For example:
    >
    > char teststr[255];
    > strcpy(teststr, "test:blah;");
    >
    > char returned_string[255];
    > strcpy(returned_string, splitstring(teststr, "test:", ";"));
    >
    > and it returned_string would = blah
    >


    If your compiler supports it, you might be interested in TR1 regular
    expressions.

    LR
    LR, Sep 2, 2008
    #4
  5. Re: Splitting a String with 2 input variables, "beginstr" and "endstr"

    On 2008-09-02 21:39, wrote:
    > On Sep 2, 2:07 pm, Erik Wikström <> wrote:
    >> On 2008-09-02 19:31, wrote:
    >>
    >>
    >>
    >> > I want to make a split string function, but it's getting complicated.

    >>
    >> > What I want to do is make a function with a String, BeginStr and an
    >> > EndStr variable, and I want it to return it in a char array.

    >>
    >> > For example:

    >>
    >> > char teststr[255];
    >> > strcpy(teststr, "test:blah;");

    >>
    >> > char returned_string[255];
    >> > strcpy(returned_string, splitstring(teststr, "test:", ";"));

    >>
    >> > and it returned_string would = blah

    >>
    >> > can someone help me?

    >>
    >> Sure, first dump the char arrays and use std::string. Then read up on
    >> std::string::find() and std::string::substr(). Start by finding BeginStr
    >> and get a substring of everything after it, then find EndStr in that and
    >> return everything in front of it.
    >>
    >> --
    >> Erik Wikström

    >
    > Ok I did that, but my string here is:
    >
    > "test1=test1; test2=test2;"
    >
    > and it finds test2, but it only finds the FIRST ";", so it's saying
    > that the EndStr is before the BeginStr, and it doesn't do this
    > correctly,
    > any remedy?


    Applying substr() can probably help, but you did not specify what you
    used as BeginStr and EndStr so I'm not sure what your problem is.

    --
    Erik Wikström
    Erik Wikström, Sep 2, 2008
    #5
  6. Guest

    Re: Splitting a String with 2 input variables, "beginstr" and"endstr"

    On Sep 2, 4:01 pm, Erik Wikström <> wrote:
    > On 2008-09-02 21:39, wrote:
    >
    >
    >
    > > On Sep 2, 2:07 pm, Erik Wikström <> wrote:
    > >> On 2008-09-02 19:31, wrote:

    >
    > >> > I want to make a split string function, but it's getting complicated..

    >
    > >> > What I want to do is make a function with a String, BeginStr and an
    > >> > EndStr variable, and I want it to return it in a char array.

    >
    > >> > For example:

    >
    > >> > char teststr[255];
    > >> > strcpy(teststr, "test:blah;");

    >
    > >> > char returned_string[255];
    > >> > strcpy(returned_string, splitstring(teststr, "test:", ";"));

    >
    > >> > and it returned_string would = blah

    >
    > >> > can someone help me?

    >
    > >> Sure, first dump the char arrays and use std::string. Then read up on
    > >> std::string::find() and std::string::substr(). Start by finding BeginStr
    > >> and get a substring of everything after it, then find EndStr in that and
    > >> return everything in front of it.

    >
    > >> --
    > >> Erik Wikström

    >
    > > Ok I did that, but my string here is:

    >
    > > "test1=test1; test2=test2;"

    >
    > > and it finds test2, but it only finds the FIRST ";", so it's saying
    > > that the EndStr is before the BeginStr, and it doesn't do this
    > > correctly,
    > > any remedy?

    >
    > Applying substr() can probably help, but you did not specify what you
    > used as BeginStr and EndStr so I'm not sure what your problem is.
    >
    > --
    > Erik Wikström


    nevermind, I think I've figured it out

    #include "stdafx.h"
    #include <iostream>
    #include <string>

    using namespace std;

    string SplitString (string str, string beginstr, string endstr);

    int main()
    {
    string teststr;
    teststr = "name=Kevin; bank=20000; failure=no; pocket=900;";

    cout << SplitString(teststr, "name", ";") << endl;
    cout << SplitString(teststr, "bank", ";") << endl;
    cout << SplitString(teststr, "failure", ";") << endl;
    cout << SplitString(teststr, "pocket", ";") << endl;
    system("pause");
    return 0;
    }

    string SplitString (string str, string beginStr, string endStr)
    {
    int begin = str.find(beginStr);
    int end = str.find(endStr);
    string result = str.substr(begin+beginStr.size()+1, end-endStr.size()-
    beginStr.size());
    return result;
    }

    --------------
    Output:
    Kirge
    20000
    no
    900
    , Sep 2, 2008
    #6
  7. Re: Splitting a String with 2 input variables, "beginstr" and "endstr"

    On 2008-09-02 22:11, wrote:
    > On Sep 2, 4:01 pm, Erik Wikström <> wrote:
    >> On 2008-09-02 21:39, wrote:
    >>
    >>
    >>
    >> > On Sep 2, 2:07 pm, Erik Wikström <> wrote:
    >> >> On 2008-09-02 19:31, wrote:

    >>
    >> >> > I want to make a split string function, but it's getting complicated.

    >>
    >> >> > What I want to do is make a function with a String, BeginStr and an
    >> >> > EndStr variable, and I want it to return it in a char array.

    >>
    >> >> > For example:

    >>
    >> >> > char teststr[255];
    >> >> > strcpy(teststr, "test:blah;");

    >>
    >> >> > char returned_string[255];
    >> >> > strcpy(returned_string, splitstring(teststr, "test:", ";"));

    >>
    >> >> > and it returned_string would = blah

    >>
    >> >> > can someone help me?

    >>
    >> >> Sure, first dump the char arrays and use std::string. Then read up on
    >> >> std::string::find() and std::string::substr(). Start by finding BeginStr
    >> >> and get a substring of everything after it, then find EndStr in that and
    >> >> return everything in front of it.

    >>
    >> >> --
    >> >> Erik Wikström

    >>
    >> > Ok I did that, but my string here is:

    >>
    >> > "test1=test1; test2=test2;"

    >>
    >> > and it finds test2, but it only finds the FIRST ";", so it's saying
    >> > that the EndStr is before the BeginStr, and it doesn't do this
    >> > correctly,
    >> > any remedy?

    >>
    >> Applying substr() can probably help, but you did not specify what you
    >> used as BeginStr and EndStr so I'm not sure what your problem is.
    >>
    >> --
    >> Erik Wikström

    >
    > nevermind, I think I've figured it out
    >
    > #include "stdafx.h"
    > #include <iostream>
    > #include <string>
    >
    > using namespace std;
    >
    > string SplitString (string str, string beginstr, string endstr);
    >
    > int main()
    > {
    > string teststr;
    > teststr = "name=Kevin; bank=20000; failure=no; pocket=900;";
    >
    > cout << SplitString(teststr, "name", ";") << endl;
    > cout << SplitString(teststr, "bank", ";") << endl;
    > cout << SplitString(teststr, "failure", ";") << endl;
    > cout << SplitString(teststr, "pocket", ";") << endl;
    > system("pause");
    > return 0;
    > }
    >
    > string SplitString (string str, string beginStr, string endStr)
    > {
    > int begin = str.find(beginStr);
    > int end = str.find(endStr);
    > string result = str.substr(begin+beginStr.size()+1, end-endStr.size()-
    > beginStr.size());
    > return result;
    > }
    >
    > --------------
    > Output:
    > Kirge
    > 20000
    > no
    > 900


    Actually it does not work correctly (and I do not understand how you
    managed to get it to look like it works). Try using "; " as the EndStr
    and you will see the problem.

    If you use substr() to get the string from the end of BeginStr to the
    end of Str and then use find() in that string it should work.

    --
    Erik Wikström
    Erik Wikström, Sep 2, 2008
    #7
  8. Guest

    Re: Splitting a String with 2 input variables, "beginstr" and"endstr"

    On Sep 2, 4:21 pm, Erik Wikström <> wrote:
    > On 2008-09-02 22:11, wrote:
    >
    >
    >
    > > On Sep 2, 4:01 pm, Erik Wikström <> wrote:
    > >> On 2008-09-02 21:39, wrote:

    >
    > >> > On Sep 2, 2:07 pm, Erik Wikström <> wrote:
    > >> >> On 2008-09-02 19:31, wrote:

    >
    > >> >> > I want to make a split string function, but it's getting complicated.

    >
    > >> >> > What I want to do is make a function with a String, BeginStr and an
    > >> >> > EndStr variable, and I want it to return it in a char array.

    >
    > >> >> > For example:

    >
    > >> >> > char teststr[255];
    > >> >> > strcpy(teststr, "test:blah;");

    >
    > >> >> > char returned_string[255];
    > >> >> > strcpy(returned_string, splitstring(teststr, "test:", ";"));

    >
    > >> >> > and it returned_string would = blah

    >
    > >> >> > can someone help me?

    >
    > >> >> Sure, first dump the char arrays and use std::string. Then read up on
    > >> >> std::string::find() and std::string::substr(). Start by finding BeginStr
    > >> >> and get a substring of everything after it, then find EndStr in that and
    > >> >> return everything in front of it.

    >
    > >> >> --
    > >> >> Erik Wikström

    >
    > >> > Ok I did that, but my string here is:

    >
    > >> > "test1=test1; test2=test2;"

    >
    > >> > and it finds test2, but it only finds the FIRST ";", so it's saying
    > >> > that the EndStr is before the BeginStr, and it doesn't do this
    > >> > correctly,
    > >> > any remedy?

    >
    > >> Applying substr() can probably help, but you did not specify what you
    > >> used as BeginStr and EndStr so I'm not sure what your problem is.

    >
    > >> --
    > >> Erik Wikström

    >
    > > nevermind, I think I've figured it out

    >
    > > #include "stdafx.h"
    > > #include <iostream>
    > > #include <string>

    >
    > > using namespace std;

    >
    > > string SplitString (string str, string beginstr, string endstr);

    >
    > > int main()
    > > {
    > >    string teststr;
    > >    teststr = "name=Kevin; bank=20000; failure=no; pocket=900;";

    >
    > >    cout << SplitString(teststr, "name", ";") << endl;
    > >    cout << SplitString(teststr, "bank", ";") << endl;
    > >    cout << SplitString(teststr, "failure", ";") << endl;
    > >    cout << SplitString(teststr, "pocket", ";") << endl;
    > >    system("pause");
    > >    return 0;
    > > }

    >
    > > string SplitString (string str, string beginStr, string endStr)
    > > {
    > >    int begin = str.find(beginStr);
    > >    int end = str.find(endStr);
    > >    string result = str.substr(begin+beginStr.size()+1, end-endStr..size()-
    > > beginStr.size());
    > >    return result;
    > > }

    >
    > > --------------
    > > Output:
    > > Kirge
    > > 20000
    > > no
    > > 900

    >
    > Actually it does not work correctly (and I do not understand how you
    > managed to get it to look like it works). Try using "; " as the EndStr
    > and you will see the problem.
    >
    > If you use substr() to get the string from the end of BeginStr to the
    > end of Str and then use find() in that string it should work.
    >
    > --
    > Erik Wikström


    Hey Erik! Thanks! It works now! Hopefully I did it right, because it
    appears to be right now, and the code looks a lot simpler.

    #include "stdafx.h"
    #include <iostream>
    #include <string>

    using namespace std;

    string stringSplit (string Str, string beginStr, string endStr);

    int main()
    {
    string str = "name=kirge; health=10; money=500;";
    cout << "Name: " << stringSplit(str, "name=", ";") << endl;
    cout << "Health: " << stringSplit(str, "health=", ";") << endl;
    cout << "Money: " << stringSplit(str, "money=", ";") << endl;
    system("pause");
    return 0;
    }

    string stringSplit (string Str, string beginStr, string endStr)
    {
    string str = Str.substr(Str.find(beginStr) + beginStr.size());
    return str.substr(0, str.find(endStr));
    }

    Output:
    Name: Kirge
    Health: 10
    Money: 500
    , Sep 2, 2008
    #8
  9. Re: Splitting a String with 2 input variables, "beginstr" and"endstr"

    On Sep 2, 1:21 pm, Erik Wikström <> wrote:
    > On 2008-09-02 22:11, wrote:
    >
    >
    >
    >
    >
    > > On Sep 2, 4:01 pm, Erik Wikström <> wrote:
    > >> On 2008-09-02 21:39, wrote:

    >
    > >> > On Sep 2, 2:07 pm, Erik Wikström <> wrote:
    > >> >> On 2008-09-02 19:31, wrote:

    >
    > >> >> > I want to make a split string function, but it's getting complicated.

    >
    > >> >> > What I want to do is make a function with a String, BeginStr and an
    > >> >> > EndStr variable, and I want it to return it in a char array.

    >
    > >> >> > For example:

    >
    > >> >> > char teststr[255];
    > >> >> > strcpy(teststr, "test:blah;");

    >
    > >> >> > char returned_string[255];
    > >> >> > strcpy(returned_string, splitstring(teststr, "test:", ";"));

    >
    > >> >> > and it returned_string would = blah

    >
    > >> >> > can someone help me?

    >
    > >> >> Sure, first dump the char arrays and use std::string. Then read up on
    > >> >> std::string::find() and std::string::substr(). Start by finding BeginStr
    > >> >> and get a substring of everything after it, then find EndStr in that and
    > >> >> return everything in front of it.

    >
    > >> >> --
    > >> >> Erik Wikström

    >
    > >> > Ok I did that, but my string here is:

    >
    > >> > "test1=test1; test2=test2;"

    >
    > >> > and it finds test2, but it only finds the FIRST ";", so it's saying
    > >> > that the EndStr is before the BeginStr, and it doesn't do this
    > >> > correctly,
    > >> > any remedy?

    >
    > >> Applying substr() can probably help, but you did not specify what you
    > >> used as BeginStr and EndStr so I'm not sure what your problem is.

    >
    > >> --
    > >> Erik Wikström

    >
    > > nevermind, I think I've figured it out

    >
    > > #include "stdafx.h"
    > > #include <iostream>
    > > #include <string>

    >
    > > using namespace std;

    >
    > > string SplitString (string str, string beginstr, string endstr);

    >
    > > int main()
    > > {
    > >    string teststr;
    > >    teststr = "name=Kevin; bank=20000; failure=no; pocket=900;";

    >
    > >    cout << SplitString(teststr, "name", ";") << endl;
    > >    cout << SplitString(teststr, "bank", ";") << endl;
    > >    cout << SplitString(teststr, "failure", ";") << endl;
    > >    cout << SplitString(teststr, "pocket", ";") << endl;
    > >    system("pause");
    > >    return 0;
    > > }

    >
    > > string SplitString (string str, string beginStr, string endStr)
    > > {
    > >    int begin = str.find(beginStr);
    > >    int end = str.find(endStr);
    > >    string result = str.substr(begin+beginStr.size()+1, end-endStr..size()-
    > > beginStr.size());
    > >    return result;
    > > }

    >
    > > --------------
    > > Output:
    > > Kirge
    > > 20000
    > > no
    > > 900

    >
    > Actually it does not work correctly (and I do not understand how you
    > managed to get it to look like it works). Try using "; " as the EndStr
    > and you will see the problem.
    >
    > If you use substr() to get the string from the end of BeginStr to the
    > end of Str and then use find() in that string it should work.
    >


    You need to do the find on the substr that begins AFTER the begin part
    is determined. Basically, you are seting "end" to the FIRST occurance
    of the end string rather than the first occurance AFTER where the
    substring of interest begins.

    It going to look (inefficiently) something like:
    int begin = str.find (beginstr);
    string result = str.substr (begin, str.size);
    int end = result.find (endstr);
    return result.substr (0, end); // or end + endstr.size depending upon
    whether you want to include endstr or not.
    Michael Angelo Ravera, Sep 2, 2008
    #9
  10. James Kanze Guest

    Re: Splitting a String with 2 input variables, "beginstr" and"endstr"

    On Sep 2, 8:07 pm, Erik Wikström <> wrote:
    > On 2008-09-02 19:31, wrote:
    > > I want to make a split string function, but it's getting
    > > complicated.


    > > What I want to do is make a function with a String, BeginStr
    > > and an EndStr variable, and I want it to return it in a char
    > > array.


    > > For example:


    > > char teststr[255];
    > > strcpy(teststr, "test:blah;");


    > > char returned_string[255];
    > > strcpy(returned_string, splitstring(teststr, "test:", ";"));


    > > and it returned_string would = blah


    > > can someone help me?


    > Sure, first dump the char arrays and use std::string. Then
    > read up on std::string::find() and std::string::substr().
    > Start by finding BeginStr and get a substring of everything
    > after it, then find EndStr in that and return everything in
    > front of it.


    As a general rule, I tend to prefer the functions in <algorithm>
    over the member functions in std::string, but I guess that's
    mainly a style issue (although once or twice, I've changed an
    std::string into an std::vector< char >, for some reason or
    another).

    More generally, before offering any advice, I'd want to know
    what the function should really do. What should the results be
    for something like splitstring( "toto:xxx;", "test:", ";" ), for
    example?

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 3, 2008
    #10
  11. James Kanze Guest

    Re: Splitting a String with 2 input variables, "beginstr" and"endstr"

    On Sep 2, 9:39 pm, wrote:
    > On Sep 2, 2:07 pm, Erik Wikström <> wrote:
    > > On 2008-09-02 19:31, wrote:


    > > > I want to make a split string function, but it's getting
    > > > complicated.


    > > > What I want to do is make a function with a String,
    > > > BeginStr and an EndStr variable, and I want it to return
    > > > it in a char array.


    > > > For example:


    > > > char teststr[255];
    > > > strcpy(teststr, "test:blah;");


    > > > char returned_string[255];
    > > > strcpy(returned_string, splitstring(teststr, "test:", ";"));


    > > > and it returned_string would = blah


    > > > can someone help me?


    > > Sure, first dump the char arrays and use std::string. Then
    > > read up on std::string::find() and std::string::substr().
    > > Start by finding BeginStr and get a substring of everything
    > > after it, then find EndStr in that and return everything in
    > > front of it.


    > Ok I did that, but my string here is:


    > "test1=test1; test2=test2;"


    > and it finds test2, but it only finds the FIRST ";", so it's
    > saying that the EndStr is before the BeginStr, and it doesn't
    > do this correctly, any remedy?


    > I'm sorry I didn't include that I needed to parse a whole line
    > of these from a text file.


    And you still haven't specified the format of that line, or what
    you're trying to do. If the goal is to create some sort of set
    of attribute value pairs, with /; */ as a separator between each
    entry, and /:/ as separator between the attribute and its value,
    this is easiest done using tr1::regex and a loop, but even
    without regex:

    std::map< std::string, std::string >
    parseAttributeValuePairs(
    std::string const& source )
    {
    typedef std::string::const_iterator
    TextIter ;
    typedef std::map< std::string, std::string >
    Result ;
    Result result ;
    TextIter current = skipSpaces( source.begin() ) ;
    TextIter end = source.end() ;
    while ( current != end ) {
    TextIter separ = std::find( current, end,
    ':' ) ;
    if ( separ == end ) {
    // Error handling here...
    }
    std::string attr( current, separ ) ;
    TextIter termin = std::find( separ + 1, end,
    ';' ) ;
    if ( termin == end ) {
    // Error handling here...
    }
    std::string value( separ + 1, termin ) ;
    result.insert( Result::value_type( attr, value ) ) ;
    current = skipSpaces( termin + 1 ) ;
    }
    return result ;
    }

    (Note that as written, the "error handling" must throw, since
    continuing in the loop in case of an error will result in
    undefined behavior. A better solution might involve skipping
    the entry, and some sort of resynchronization, with memorization
    of the error(s), and returning a Fallible.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 3, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page