Erase all occurences of a word from a string [duplicate] - c++

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.

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

Remove Bracket [*TESTABC*] along with content from String prefix in java [duplicate]

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 **

How to replace all occurrences of a character with a given string [duplicate]

This question already has answers here:
How to replace all occurrences of a character in string?
(17 answers)
Replace part of a string with another string
(17 answers)
Closed 5 years ago.
I want to replace all occurrences of '$' with "$$". Currently I am using string::find to check if '$' is present in string or not. then I am using for loop and checking every character if it matches with '$'. If a character matches with $ then I am using string::replace to replace it.
Is there any other effective method to do this in c++? without traversing entire string with less complexity?
I don't know a standard function which replaces ALL occurrences of a certain string with another string. The replace-functions either replace all occurrences of a specific character by another character, or replace a single range of characters with another range.
So I think you cannot avoid iterating through the string on your own.
In your specific case, it might be easier, as you just have to insert an additional $ for every $-character you find. Note the special measure avoiding an endless loop, which would happen if one doubled also the $-value just inserted again and again:
int main() {
string s = "this $ should be doubled (i.e. $), then. But $$ has been doubled, too.";
auto it = s.begin();
while (it != s.end()) {
if (*it == '$') {
it = s.insert(it, '$');
it += 2;
}
else {
it++;
}
}
cout << s;
}

Regex permutations without repetition [duplicate]

This question already has answers here:
How to find all permutations of a given word in a given text?
(6 answers)
Closed 7 years ago.
I need a RegEx to check if I can find a expression in a string.
For the string "abc" I would like to match the first appearance of any of the permutations without repetition, in this case 6: abc, acb, bac, bca, cab, cba.
For example, in this string "adesfecabefgswaswabdcbaes" it'd find a coincidence in the position 7.
Also I'd need the same for permutations without repetition like this "abbc". The cases for this are 12: acbb, abcb, abbc, cabb, cbab, cbba, bacb, babc, bcab, bcba, bbac, bbca
For example, in this string "adbbcacssesfecabefgswaswabdcbaes" it'd find a coincidence in the position 3.
Also, I would like to know how would that be for similar cases.
EDIT
I'm not looking for the combinations of the permutations, no. I already have those. WHat I'm looking for is a way to check if any of those permutations is in a given string.
EDIT 2
This regex I think covers my first question
([abc])(?!\1)([abc])(?!\2|\1)[abc]
Can find all permutations(6) of "abc" in any secuence of characters.
Now I need to do the same when I have a repeated character like abbc (12 combinations).
([abc])(?!\1)([abc])(?!\2|\1)[abc]
You can use this without g flag to get the position.See demo.The position of first group is what you want.
https://regex101.com/r/nS2lT4/41
https://regex101.com/r/nS2lT4/42
The only reason you might "need a regex" is if you are working with a library or tool which only permits specifying certain kinds of rules with a regex. For instance, some editors can be customized to color certain syntactic constructs in a particular way, and they only allow those constructs to be specified as regular expressions.
Otherwise, you don't "need a regex", you "need a program". Here's one:
// are two arrays equal?
function array_equal(a1, a2) {
return a1.every(function(chr, i) { return chr === a2[i]; });
}
// are two strings permutations of each other?
function is_permutation(s1, s2) {
return array_equal(s1.split('').sort(), s2.split('').sort());
}
// make a function which finds permutations in a string
function make_permutation_finder(chars) {
var len = chars.length;
return function(str) {
for (i = 0; i < str.length - len; i++) {
if (is_permutation(chars, str.slice(i, i+len))) return i;
}
return -1;
};
}
> finder = make_permutation_finder("abc");
> console.log(finder("adesfecabefgswaswabdcbaes"));
< 6
Regexps are far from being powerful enough to do this kind of thing.
However, there is an alternative, which is precompute the permutations and build a dynamic regexp to find them. You did not provide a language tag, but here's an example in JS. Assuming you have the permutations and don't have to worry about escaping special regexp characters, that's just
regexp = new RegExp(permuations.join('|'));

spliting in c++ like what we do in c# [duplicate]

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