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
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
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 4 years ago.
Improve this question
I want to reverse a substring in a string. For example
string x = "foobar";
So I want to reverse foo to be oof so x would be "oofbar";
Here's a sample of what I have tried:
string x = "foobar";
size_t pos = x.find("foo");
reverse(x.begin() + k, s.end()); // I got stuck here.
For a simple case as
string x = "foobar";
you can use
std::reverse(x.begin(), x.begin() + 3); // reverse the first three letters.
If "foo" is embedded in the string, i.e. it is not at the start of the string, you'll have to find its location first.
string x = "whoknowswhereitisfoobar";
auto loc = x.find("foo");
if ( loc != std::string::npos )
{
std::reverse(x.begin() + loc, x.begin() + loc + 3);
}
If you don't want to hard code the number 3, you can use std::string::size():
string hay = "whoknowswhereitisfoobar";
string needle = "foo";
auto loc = hay.find(needle);
if ( loc != std::string::npos )
{
std::reverse(hay.begin() + loc, hay.begin() + loc + needle.size());
}
See Live Here
Given auto x = "foobar"s and const auto value = "foo"s, I'd probably use iterators just cause of their convenience:
const auto it = search(cbegin(x), cend(x), cbegin(value), cend(value));
if(it != cend(x)) {
x.replace(it, next(it, size(value)), crbegin(value), crend(value));
}
Live Example
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
}
}
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");