Method of removing punctuation or the last character of a Java string technique

  • 2020-04-01 04:18:19
  • OfStack

Remove all punctuation from the string


str = str.replaceAll("[\pP ' ' "" ]", "");

In this case, Unicode encodings are used, and Unicode encodings do not simply define an encodings for a character, but also categorize it.

The lowercase p in \pP means property, represents Unicode property, and is used as a prefix for Unicode positive expressions.

Capital P represents one of the seven character attributes of the Unicode character set: the punctuation character.
The other six are

L: letter; M: mark symbol (usually not seen separately); Z: separator (such as space, newline, etc.); S: symbols (such as mathematical symbols, currency symbols, etc.); N: Numbers (e.g. Arabic numerals, Roman numerals, etc.); C: other characters

Regular expression data for Unicode in Java is provided by the Unicode organization. Unicode regular expression standard (all child properties can be found)
http://www.unicode.org/reports/tr18/
http://www.unicode.org/Public/UNIDATA/UnicodeData.txt
This text document has a single line of characters, the first column is the Unicode encoding, the second column is the character name, and the third column is the Unicode attribute,
And some other character information.


Deletes the last character of the string
String:


string s = "1,2,3,4,"

Delete the last ","

Methods:
1. Use the Substring


s = s.Substring(0,s.Length - 1)

2. Use the RTrim


s = s.ToString().RTrim(',')

3. Use TrimEnd


s=s.TrimEnd(',')
//If you want to delete "4," you need to do this
char[] MyChar = {'4',','};
s = s.TrimEnd(MyChar);
//s = "1,2,3

4. Use lastIndexOf() and deleteCharAt()


int index = sb.toString().lastIndexOf(',');
sb.deleteCharAt(index);


Related articles: