Remove Characters from a String

How to remove single character from a String by index

You can also use the StringBuilder class which is mutable.

StringBuilder sb = new StringBuilder(inputString);

It has the method deleteCharAt(), along with many other mutator methods.

Just delete the characters that you need to delete and then get the result as follows:

String resultString = sb.toString();

This avoids creation of unnecessary string objects.

Remove all occurrences of char from string

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

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

How to delete a character from a string using Python

In Python, strings are immutable, so you have to create a new string. You have a few options of how to create the new string. If you want to remove the 'M' wherever it appears:

newstr = oldstr.replace("M", "")

If you want to remove the central character:

midlen = len(oldstr) // 2
newstr = oldstr[:midlen] + oldstr[midlen+1:]

You asked if strings end with a special character. No, you are thinking like a C programmer. In Python, strings are stored with their length, so any byte value, including \0, can appear in a string.

How to remove special characters from a string?

That depends on what you define as special characters, but try replaceAll(...):

String result = yourString.replaceAll("[-+.^:,]","");

Note that the ^ character must not be the first one in the list, since you'd then either have to escape it or it would mean "any but these characters".

Another note: the - character needs to be the first or last one on the list, otherwise you'd have to escape it or it would define a range ( e.g. :-, would mean "all characters in the range : to ,).

So, in order to keep consistency and not depend on character positioning, you might want to escape all those characters that have a special meaning in regular expressions (the following list is not complete, so be aware of other characters like (, {, $ etc.):

String result = yourString.replaceAll("[\\-\\+\\.\\^:,]","");


If you want to get rid of all punctuation and symbols, try this regex: \p{P}\p{S} (keep in mind that in Java strings you'd have to escape back slashes: "\\p{P}\\p{S}").

A third way could be something like this, if you can exactly define what should be left in your string:

String  result = yourString.replaceAll("[^\\w\\s]","");

This means: replace everything that is not a word character (a-z in any case, 0-9 or _) or whitespace.

Edit: please note that there are a couple of other patterns that might prove helpful. However, I can't explain them all, so have a look at the reference section of regular-expressions.info.

Here's less restrictive alternative to the "define allowed characters" approach, as suggested by Ray:

String  result = yourString.replaceAll("[^\\p{L}\\p{Z}]","");

The regex matches everything that is not a letter in any language and not a separator (whitespace, linebreak etc.). Note that you can't use [\P{L}\P{Z}] (upper case P means not having that property), since that would mean "everything that is not a letter or not whitespace", which almost matches everything, since letters are not whitespace and vice versa.

Additional information on Unicode

Some unicode characters seem to cause problems due to different possible ways to encode them (as a single code point or a combination of code points). Please refer to regular-expressions.info for more information.

remove '$' characters from a string

yup, that's a special character for pattern matching. you need to escape it with the % symbol.

local s = 'asdf$erer$iiuq'
print(s:gsub('%$', ''))

> asdfereriiuq 2

Removing characters from string in R

You need to use str_replace_all to remove the multiple accounts of whitespace characters. Why not use base R to remove these characters instead of loading the stringr package?

gsub('[\t\n]', '', mydata)

Remove character based on condition and position in string - R

While akrun's approach is the one that should be used. Here is stringr composition:

  1. with word(df, 1) we take the left part of the string
  2. with word(df, -1) we take the right part (here we use
    2a. str_remove_all with regex ^0+ to remove leading zeros.
  3. Finally we use str_c to combine both parts:
library(stringr)
str_c(word(df,1), str_remove_all(word(df, -1), '^0+'))
[1] "2015808"    "201341"     "20155"      "2015301585" "2015311585" "2014380096" "2013100041"

how to remove Characters from a string in react.js

you can use regex and string.replace

string.replace(/[|&;$%@"<>()+,]/g, "");

just put whatever string you want to ignore inside the rectangular brackets, i.e - string.replace(/[YOUR_IGNORED_CHARACTERS]/g, "")

you can read more about regex here



Related Topics



Leave a reply



Submit