C++ Strings and delimiters [duplicate] - c++

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 7 years ago.
So i'm doing a program where i input some data into a file and read from it. The problem is i dont know how to handle this. I read from the file and recieve a string containing alot of different data that is seperated by a delimiter "|".
string data ="FirstName|LastName|Signature|Height";
So my question is, is there a way to seperate this string nicely and store each of the values in a seperate variable?
p.s I have tried this so far. I did find this function subrt() and find() which i could use in order to find the delimiter and take out a value but it doesnt give the correct value for me so i think i'm doing something wrong with it. Only the fname value is correct.
const string DELIM = "|";
string fname = data.substr(0, data.find(DELIM));
string lname = data.substr(1, data.find(DELIM));
string signature = data.substr(2, data.find(DELIM));
string height = data.substr(3, data.find(DELIM));

You did not understand how substr() works. The first parameter is not the number of character found, it is the index at which to start. See the doc. You should do the same for the find function. Something like that:
string const DELIM = "|";
auto const firstDelim = data.find(DELIM);
auto const secondDelim = data.find(DELIM, firstDelim + 1); // begin after the first delim
// and so on
auto fname = data.substr(0, firstDelim);
auto lname = data.substr(firstDelim + 1, secondDelim);
// and so on

Related

Replace text \n with actual new line. (C++) [duplicate]

This question already has answers here:
How to find and replace all occurrences of a substring in a string?
(9 answers)
Replace substring with another substring C++
(18 answers)
How to find and replace string?
(11 answers)
How do I replace all instances of a string with another string?
(6 answers)
Closed 6 months ago.
I'm using C++ and I have a problem. Instead of creating a new line it prints \n. My Code:
std::string text;
std::cout << text;
It prints:Hello\nWorld
It was supposed to read \n as a new line and print something like this:
"Hello
World"
So i've tried to use replace(text.begin(), text.end(), '\n', 'a') for testing purposes and nothing happened. It contiuned to print Hello\nWorld
std::replace() won't work in this situation. When called on a std::string, it can replace only single characters, but in your case your string actually contains 2 distinct characters '\' and 'n'. So you need to use std::string::find() and std::string::replace() instead, eg:
string::size_type index = 0;
while ((index = str.find("\\n", index)) != string::npos) {
str.replace(index, 2, "\n");
++index;
}

how to get specific data from a txt file in c++ [duplicate]

This question already has answers here:
use getline and while loop to split a string
(3 answers)
Closed 1 year ago.
it's my frist publication and i have a question-
I have a .txt file that I want to separate by the ",".
Example:
example1,example2,example3,example4
I want to get example1 and store in an array and so on for example2 , example3, example4.
string[0] = example1
string[1] = example2
etc....
getline by default will split input by end-line chatracter, but can do for any character, including ',' - see example (using a stringstream):
std::string input = "word1,word2,word3";
std::stringstream sstream(input);
std::string word;
while(std::getline(sstream, word, ',')) {
std::cout << word << '\n';
}

C++ - How can i split up 3 lines safed in string called "result" and create a oneline "synonym" for all 3 strings. - C++ [duplicate]

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";

Verify and cut a string using regexp in matlab

I have the following string:
{'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921}
I would like to verify the format of the string that the word 'variable' is the second word and i would like to retrive the string after the last '/' in the 3rd string (In this example 'D_foo').
how could i verify this and retrive the sting i search?
I tried the following:
regexp(str,'{''\w+'',{''variable'',''([(a-z)|(A-Z)|/|_])+')
without success
REMARK
The string to analysis is not splited after the komma, it is only due to length of the string.
EDIT
my string is:
'{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
and not a cell, which could be understood. I added the sybol ' at the start and end of the string to symbolizied that it is a string.
I realise that you mention using regexp in the question, but I'm not sure if this is a requirement? If other solutions are acceptable you could try this:
str='{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
parts1=textscan( str, '%s','delimiter',{',','{','}'},'MultipleDelimsAsOne',1);
parts2=textscan( parts1{1}{3}, '%s','delimiter',{'/',''''},'MultipleDelimsAsOne',1);
string=parts2{1}{end}
match=strcmp(parts1{1}{2},'variable')
To answer the first part of your question, you can write this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %this holds the cell containing the two strings
if cmpstr(temp{1}(1), 'variable')
%do stuff
end
For the second part you can do this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %like before, this contains the cell
temp = temp{1}(2); %this picks out the second string in the cell
temp = char(temp); %turns the item from a cell to a string
res = strsplit(temp, '/'); %splits the string where '/' are found, res is an array of strings
string = res(3); %assuming there will always be just 2 '/'s.

How to split a string? [duplicate]

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.