Remove All Occurrences of Char from String

Remove all occurrences of char from string

Try using the overload that takes CharSequence arguments (eg, String) rather than char:

str = str.replace("X", "");

Remove all occurrences of char . from string in Java

The first argument of replaceAll is a regex pattern. Since . means "any character", all the characters are removed. In order to refer to the actual . character, you need to escape it:

originalString = originalString.replaceAll("\\.", "");

How to remove all the occurrences of a char in c++ string

Basically, replace replaces a character with another and '' is not a character. What you're looking for is erase.

See this question which answers the same problem. In your case:

#include <algorithm>
str.erase(std::remove(str.begin(), str.end(), 'a'), str.end());

Or use boost if that's an option for you, like:

#include <boost/algorithm/string.hpp>
boost::erase_all(str, "a");

All of this is well-documented on reference websites. But if you didn't know of these functions, you could easily do this kind of things by hand:

std::string output;
output.reserve(str.size()); // optional, avoids buffer reallocations in the loop
for(size_t i = 0; i < str.size(); ++i)
if(str[i] != 'a') output += str[i];

Remove all occurrences of a character except the first one in string with javascript

If it's always at the beginning, you could do something like this:

const result = '# A # B # C'.replace(/(?!^)#/g, '');

console.log(result);

javascript delete all occurrences of a char in a string

Regular expressions [xkcd] (I feel like him ;)):

s = s.replace(/[\[\]&]+/g, '');

Reference:

  • MDN - string.replace
  • MDN - Regular Expressions
  • http://www.regular-expressions.info/

Side note:

JavaScript's replace function only replaces the first occurrence of a character. So even your code would not have replaced all the characters, only the first one of each. If you want to replace all occurrences, you have to use a regular expression with the global modifier.

How to remove all occurrences of any given character from string?

The method String.replace() doesn't change the string, it creates a new string. Return the result of the replace.

In addition, since you've used the g flag in the regex, it will replace all occurrences in the string, so you don't need the for loop.

function removeFromString(mystring, char) {

const regex = new RegExp(char, 'g');

return mystring.replace(regex, '');

}

console.log(removeFromString('Hello How are you', 'o'));

Remove all instances of a character from string

You can use Regular Expression( here i have assumed that character is x):

string result = Regex.Replace( input , "x(?=\\S)" , "");

Live Demo. Please check that it uses very few steps finding it.

Removing all occurrences of a character from a string in C++

If I have understood the description of the assignment correctly,
then you need something like the following:

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <cctype>

void removeChar( std::string::size_type &n, std::string &str, const std::string &s, char c )
{
str.clear();

c = std::toupper( ( unsigned char )c );

auto equal_to_c = [&c]( const auto &item )
{
return std::toupper( ( unsigned char )item ) == c;
};

std::remove_copy_if( std::begin( s ), std::end( s ),
std::back_inserter( str ),
equal_to_c );

n = s.size() - str.size();
}

int main()
{
std::string::size_type n = 0;
std::string str;

removeChar( n, str, "This is a silly assignment", 's' );

std::cout << "n = " << n << ", str = " << str << '\n';

return 0;
}

The program output is:

n = 5, str = Thi i a illy aignment


Related Topics



Leave a reply



Submit