简体   繁体   中英

String manipulation first character and number

I have a string "shyam123-rohan-jack75"

I want to have output like "S123-r-j75"

Basically if, Print the first character of string and if there is a intiger part in a word we have to print it as well.

Using String#replaceAll :

String input = "shyam123-rohan-jack75";
input = input.replaceAll("\\b([A-Za-z])[A-Za-z]*", "$1");
System.out.println(input);

This prints:

s123-r-j75

This regex approach works by matching and capturing the first alphabet character following a word boundary. It also matches all following alphabet characters (but not numbers), replacing with just the first captured alphabet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM