This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to split a string in C++?
Splitting a C++ std::string using tokens, e.g. “;”
think I have this string :
string a = "hello,usa,one,good,bad";
I want to splitting this string with ,
so I need a array of string like here :
string *a ; a = { hello , usa , one , good , bad }
what shoudl I do ?
This simple AXE parser will do it:
std::vector<std::string> strings;
auto split = *(*(axe::r_any() - ',') >> e_push_back(strings));
split(a.begin(), a.end());
If you really don't want to code this on your own you can do a web search for "c++ tokenize string" and take, for example, a look here: CPPHOWTO
Related
This question already has answers here:
Parse (split) a string in C++ using string delimiter (standard C++)
(33 answers)
Closed 2 years ago.
So, Hello Guys.
I have a function, which reads the Diskdrive Serialnumber.
I get a output like this:
SerialNumber A6PZD6FA 1938B00A49382 0000000000000
(thats not my serialnumber, just the format)
So now, i want to split the 3 numbers, or strings, however i call it you know what i mean, and save it in in three independent strings.
string a = {A6PZD6FA this value} string b = {1938B00A49382 this value} string c = {0000000000000 this value}
After that, i want to create a oneline "synonym" for all 3 strings. So i mean,
string synonym = 04930498SV893948AJVVVV34
something like this.
If you have the original text in a string variable, you can use a std::istringstream to parse it into constituent parts:
std::string s = "SerialNumber A6PZ etc etc...";
std::istringstream iss{s};
std::string ignore, a, b, c;
if (iss >> ignore >> a >> b >> c) {
std::string synonym = a + b + c;
...do whatever with synonym...
} else
std::cerr << "your string didn't contain four whitespace separated substrings\n";
This question already has answers here:
Java replace all square brackets in a string
(8 answers)
Closed 4 years ago.
I am having string as below and want prefix to be removed [*TESTABC*]
String a = "[*TESTABC*]test#test.com";
Expected result: test#test.com
I tried below code but it is removing the bracklets only. I need to remove the inner content also.
The string with come in this pattern only. Please help.
String s = "[*TESTABC*]test#test.com";
String regex = "\\[|\\]";
s = s.replaceAll(regex, "");
System.out.println(s); //*TESTABC*test#test.com
int index;
String s = "[*TESTABC*]test#test.com";
index=s.indexOf(']');
s=s.substring(index+1);
System.out.println(s);
**i solve your question according to your problem and output **
This question already has answers here:
How to extract integers from a string separated by spaces in Python 2.7?
(4 answers)
Closed 5 years ago.
I have a string like below,
string str ="3rd Floor, aaa Mall, bbb, cccc, dd, ee, 123456 ,ff"
I have to extract "123456" from above string.
I am using below code to extract ,
p = re.findall(r'\b\d{6}\b',value)
and output is
[u'123456']
I have tried int(p) to get output as 123456 but its not working.
Try:
ints = []
for s in p:
ints.append(int(s))
Then use ints as you like.
UPDATE
If you are sure there will be one and only one integer, then do:
this_is_an_int = int(p[0])
This question already has answers here:
Replace part of a string with another string
(17 answers)
Closed 7 years ago.
I have a string such as:
AAAbbbbbAAA
I'd like to remove all the occurances of a pattern AAA to get:
bbbbb
The pattern can occur anywhere in the string.
Given:
string yourstring("AAAbbbAAA");
string removestring("AAA");
You could simply run something like this multiple times on your string:
yourstring.erase(yourstring.find(removestring), removestring.length());
Of course you will have to check that string::find actually finds an occurence before using string::erase.
Here is the code. It's not very much efficient, but works well, and is a tiny code.
string h = "AAAbbbAAAB";
int pos = h.find("AAA");
while(pos!=-1)
{
h.replace(pos, 3, "");
pos = h.find("AAA");
}
cout << h << endl;
It only works if you know the pattern. If it doesn't what you want, maybe you're looking for a pattern matching algorithm, like KMP.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is There A Built-In Way to Split Strings In C++?
i have one string variable which contains:
string var = "this_is.that_alos" how i can separate only "that_alos" from it.
i.e. i need only second part after "that_alos"
std::string var = "this_is.that_alos";
std::string part = var.substr(var.find_first_of('.'));
If you want to exclude the '.', just add 1:
std::string part = var.substr(var.find_first_of('.') + 1);
In C, the usual function is strchr(3):
char * second_half = strchr(var, '.');
Of course, this is just a new pointer to the middle of the string. If you start writing into this string, it changes the one and only copy of it, which is also pointed to by char *var. (Again, assuming C.)
std::string var = "this_is.that_alos";
std::string::iterator start = std::find(var.begin(), var.end(), '.')
std::string second(start + 1, var.end());
But you should check if start + 1 is not var.end(), if var don't contain any dot.
If you don't want to use string indexing or iterators, you can use std::getline
std::string var = "this_is.that_alos";
std::istringstream iss(var);
std::string first, second;
std::getline(iss, first, '.');
std::getline(iss, second, '.');
The easiest solution would be to use boost::regex (soon to be
std::regex). It's not clear what the exact requirements are,
but they should be easy to specify using a regular expression.
Otherwise (and using regular expressions could be considered
overkill for this), I'd use the usual algorithms, which work for
any sequence. In this case, std::find will find the first '.',
use it with reverse iterators to find the last, etc.
--
James Kanze
Use strtok for splitting the String, with the Special character as "."
for ex
string var = "this_is.that_alos";
string Parts[5] = strtok(&var[0],".");
string SubPart = Parts[1];
Hope it will help U.