473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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
12 1892
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joe Fallon | last post by:
I know that String.Format has the ability to format a string using placeholders like {0}, {1}, {2} etc. Is there a way to make a template that uses variables like <% MyVar1 %> instead of {0}?...
2
by: David Garamond | last post by:
When a timestamp string input contains a timezone abbreviation (CDT, PST, etc), which timezone offset is used? The input date's or today date's? The result on my computer suggests the latter. #...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
11
by: Rubic | last post by:
I was a little surprised to recently discover that datetime has no method to input a string value. PEP 321 appears does not convey much information, but a timbot post from a couple years ago...
4
by: priyanka | last post by:
Hi, I want to input a string from command line. I use the following program to input the string. #include<stdio.h> int main(){ char input;
1
by: kevinliu23 | last post by:
Hi guys, Python newbie here for some expert help. So basically I want to design a menu system that waits for a string input. I'm not sure what the best way of going about this is. The current...
8
by: merrittr | last post by:
I have been having troubles getting "string" input pushed on to the stack in the assn1. The funny thing is I can push a literal on no problem so push("push i 0"); push("push i 1");...
3
by: Energizer100 | last post by:
Hey. I'm new to Java and I'm trying to make a grades program for a class assignment. This is what I have so far. import java.util.Scanner; import static java.lang.System.out; public class...
2
by: Alan Bak | last post by:
I have a text file that I am reading in with perl line by line and breaking into variables for processing. The lines are being broken on white spaces using split. Here are a couple of lines from...
1
by: elamunyon | last post by:
I have a coldfusion search that is pulling from a database and it does not seem to be finding the input variables. I keep getting no reults from my input, when I know that it should be finding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.