By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On 2008-09-02 19:31, ke***********@gmail.com 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
Sep 2 '08 #2

P: n/a
On Sep 2, 2:07*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 19:31, kevineller...@gmail.com 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 '08 #3

P: n/a
LR
ke***********@gmail.com 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
Sep 2 '08 #4

P: n/a
On 2008-09-02 21:39, ke***********@gmail.com wrote:
On Sep 2, 2:07 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2008-09-02 19:31, kevineller...@gmail.com 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
Sep 2 '08 #5

P: n/a
On Sep 2, 4:01*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 21:39, kevineller...@gmail.com wrote:
On Sep 2, 2:07 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 19:31, kevineller...@gmail.com 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 '08 #6

P: n/a
ke***********@gmail.com wrote:
[..] 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
Wow! The string contains 'Kevin' and the output is 'Kirge'? It must be
doing some fancy translation along the way... :-)
20000
no
900
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 2 '08 #7

P: n/a
On 2008-09-02 22:11, ke***********@gmail.com wrote:
On Sep 2, 4:01 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2008-09-02 21:39, kevineller...@gmail.com wrote:
On Sep 2, 2:07 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 19:31, kevineller...@gmail.com 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
Sep 2 '08 #8

P: n/a
On Sep 2, 4:21*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 22:11, kevineller...@gmail.com wrote:
On Sep 2, 4:01 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 21:39, kevineller...@gmail.com wrote:
On Sep 2, 2:07 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 19:31, kevineller...@gmail.com 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 '08 #9

P: n/a
On Sep 2, 1:21*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 22:11, kevineller...@gmail.com wrote:


On Sep 2, 4:01 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 21:39, kevineller...@gmail.com wrote:
On Sep 2, 2:07 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 19:31, kevineller...@gmail.com 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.

Sep 2 '08 #10

P: n/a
ke***********@gmail.com wrote:
[..]
#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
It gets funnier every time you post. Now it uppercases the name without
any instruction to do so... Could it be 'stdafx.h' header contains some
definitions for the output to be uppercased if immediately preceded by
"Name:"? :-)

Hint: you should consider copying and pasting your output instead of
re-typing it. Otherwise it's *cheating*.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 2 '08 #11

P: n/a
Victor Bazarov wrote:
ke***********@gmail.com wrote:
<split>
>--------------
Output:
Kirge

Wow! The string contains 'Kevin' and the output is 'Kirge'? It must be
doing some fancy translation along the way... :-)
Aparrently it's split into Klingon and Earth. :)
Sep 2 '08 #12

P: n/a
On Sep 2, 9:39 pm, kevineller...@gmail.com wrote:
On Sep 2, 2:07 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-09-02 19:31, kevineller...@gmail.com 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:ja*********@gmail.com
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
Sep 3 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.