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 g
lobal 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
Synchronizing on String Objects in Java
How to Invoke a Linux Shell Command from Java
Hibernate Opensession() VS Getcurrentsession()
How Is an Overloaded Method Chosen When a Parameter Is the Literal Null Value
Modify Request Parameter with Servlet Filter
Javafx Controller Class Not Working
Org.Apache.Tomcat.Util.Bcel.Classfile.Classformatexception: Invalid Byte Tag in Constant Pool: 15
What Is the Best Java Image Processing Library/Approach
Java Wait and Notify: Illegalmonitorstateexception
How to Get the Subscription Information from Google Play Android Developer API
Selenium 2.53 Not Working on Firefox 47
Java List.Add() Unsupportedoperationexception
Odd Behavior When Java Converts Int to Byte
Importing Two Classes with Same Name. How to Handle
What Does Java Option -Xmx Stand For
Main Thread VS. UI Thread in Java
Quickly Read the Last Line of a Text File
How to Respond with an Http 400 Error in a Spring MVC @Responsebody Method Returning String