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

K

kevineller794

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 =)
 
E

Erik Wikström

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.
 
K

kevineller794

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.
 
L

LR

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
 
E

Erik Wikström

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?

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.
 
K

kevineller794

On 2008-09-02 19:31, (e-mail address removed) 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?

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.

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
 
E

Erik Wikström

On 2008-09-02 19:31, (e-mail address removed) 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?

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.

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.
 
K

kevineller794

On 2008-09-02 21:39, (e-mail address removed) wrote:
On 2008-09-02 19:31, (e-mail address removed) 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.
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.

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
 
M

Michael Angelo Ravera

On 2008-09-02 21:39, (e-mail address removed) wrote:
On 2008-09-02 19:31, (e-mail address removed) 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.
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.
 
J

James Kanze

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?
 
J

James Kanze

On 2008-09-02 19:31, (e-mail address removed) 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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top