Split the end of a string in C++ using string delimiter (,) [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have the below format of a string:
str1 = "a, b, c, d, e";
str2 = "aa, ba, ca, da, essd";
str2 = "aass, bsda, cads, dsda, esssdsd";
I want to extract the end of the string after splitting --> e, essd, esssdsd.

Assuming these are std::strings, I'd use rfind to find the last occurrence of the delimiter, and then take a substring from there. E.g.:
size_t index = str.rfind(", ");
string last_element = str.substr(index + 2); // 2 is the size of the delimiter

Related

get substring regEx [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How can I get from string "C27_W112_V113_Table__6__1" string "6" or from string "C27_W120_V153_Table__22__1" string "22". thx
You can achieve it by two ways
Regex approach:
const re = /(\d+)__\d+$/;
let values = [
'C27_W112_V113_Table__6__1',
'C27_W120_V153_Table__22__1'
];
values.forEach(str => console.log(str.match(re)[1]));
string manipulation (I assume the value is always at the same place):
values.forEach(str => {
let reversed = str.split('').reverse().join('');
let index = reversed.indexOf('__');
console.log(reversed.slice(index+2, reversed.indexOf('__', index+1)));
});
Here a snippet
const re = /(\d+)__\d+$/;
let values = [
'C27_W112_V113_Table__6__1',
'C27_W120_V153_Table__22__1'
];
values.forEach(str => console.log(str.match(re)[1]));
console.log('--------');
values.forEach(str => {
let reversed = str.split('').reverse().join('');
let index = reversed.indexOf('__');
console.log(reversed.slice(index+2, reversed.indexOf('__', index+1)));
});

Regex find all first unique occurences of character in a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have following string
1,2,3,a,b,c,a,b,c,1,2,3,c,b,a,2,3,1,
I would like to get only the first occurrence of any number without changing the order. This would be
1,2,3,a,b,c,
With this regex (found # https://stackoverflow.com/a/29480898/9307482) I can find them, but only the last occurrences. And this reverses the order.
(\w)(?!.*?\1) (https://regex101.com/r/3fqpu9/1)
It doesn't matter if the regex ignores the comma. The order is important.
Regular expression is not meant for that purpose. You will need to use an index filter or Set on array of characters.
Since you don't have a language specified I assume you are using javascript.
Example modified from: https://stackoverflow.com/a/14438954/1456201
String.prototype.uniqueChars = function() {
return [...new Set(this)];
}
var unique = "1,2,3,a,b,c,a,b,c,1,2,3,c,b,a,2,3,1,".split(",").join('').uniqueChars();
console.log(unique); // Array(6) [ "1", "2", "3", "a", "b", "c" ]
I would use something like this:
// each index represents one digit: 0-9
const digits = new Array(10);
// make your string an array
const arr = '123abcabc123cba231'.split('');
// test for digit
var reg = new RegExp('^[0-9]$');
arr.forEach((val, index) => {
if (reg.test(val) && !reg.test(digits[val])) {
digits[val] = index;
}
});
console.log(`occurrences: ${digits}`); // [,0,1,2,,,,....]
To interpret, for the digits array, since you have nothing in the 0 index you know you have zero occurrences of zero. Since you have a zero in the 1 index, you know that your first one appears in the first character of your string (index zero for array). Two appears in index 1 and so on..
A perl way to do the job:
use Modern::Perl;
my $in = '4,d,e,1,2,3,4,a,b,c,d,e,f,a,b,c,1,2,3,c,b,a,2,3,1,';
my (%h, #r);
for (split',',$in) {
push #r, $_ unless exists $h{$_};
$h{$_} = 1;
}
say join',',#r;
Output:
4,d,e,1,2,3,a,b,c,f

Extract int value and string from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I would like to use string class, to extract some information from the string.
Given the string: <12,Apple>,<20,Orange>,<49,iPhone>
I would like to let 12, 20, 49 to a int array.
that means a[0] = 12, a[1] = 20, a[2] = 49.
And let Apple, Orange, iPhone to a String array.
that means b[0] = "Apple", b[1] = "Orange" b[2] = "iPhone"
How should I do?
Assume the string follows the format <int,string>,.... Please find the pseudo-code below:
Loop through the string `str` and
{
smaller_sign_pos = str.find('<', prev_pos)
entry_comma_pos = str.find(',', smaller_sign_pos+1)
greater_sign_pos = str.find('>', entry_comma_pos+1)
if (all pos values are not `npos`)
{
int_value = atoi(str.substr(smaller_sign_pos+1, entry_comma_pos-smaller_sign_pos-1))
str_value = str.substr(entry_comma_pos+1, greater_sign_pos-entry_comma_pos-1)
prev_pos = greater_sign_pos+1
append int_value to int array
append str_value to string array
optional: you can check if the comma after '>' exists
}
else
{
break or set the end of loop flag
}
}

C++ find whitespaces in substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to know how can I find empty or whitespaces within substring in C++. For example:
string str = "( )"; // or str = "()"
Here, I want to make sure there is always something in between parenthesis. Function isspace() takes only one character so I have to search in loop. Is there any better way to do this? Thanks for your help.
You can use std::string::find() to find the ( and ) characters, then use std::string::find_first_not_of() to check for any non-whitespace characters between those indexes.
string str = "( )"; // or str = "()"
string::size_type idx1 = str.find("(");
if (idx1 != string::npos) {
++idx1;
string::size_type idx2 = str.find(")", idx1);
if (idx2 != string::npos) {
string tmp = str.substr(idx, idx2-idx1);
string::size_type idx3 = tmp.find_first_not_of(" \t\r\n");
if (idx3 != string::npos) {
...
}
}
}

Split multiple Words in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a String like this
String s1="1 AND 1 OR 1";
I want split with AND OR and my result should be 1,1,1
I am using JAVA /eclipse
package com.test;
String s1="1 AND 1 OR 1";
String[] splits=s1.split("[AND\OR]");
for (int i = 0; i < splits.length; i++) {
System.out.println(splits[i]);
}
}
}
Can I get any help how to do this?
Any Help appreciated
I am unsure about the language, but for C# you can use the following:
string s1 = "1 AND 1 OR 1";
string s2 = s1.Replace("AND", ",").Replace("OR", ",");
Console.WriteLine(s2);
Which doesn't use regular expressions.
If you want an array, you can use the following:
string s1 = "1 AND 1 OR 1";
string[] s2 = Regex.Split(s1.Replace(" ", string.Empty), "AND|OR");
In Java you can replace using the same mechanism:
String s1 = "1 AND 1 OR 1";
String s2 = s1.replace("AND", ",").replace("OR", ",");
System.out.println(s2);
And to get an array:
String s1="1 AND 1 OR 1";
String[] s2 = s1.replace(" ", "").split("AND|OR");