-1

String name1 = " shashi";

Output: name1:" Shashi";

String name2 = "@@@shashi";

Output: name2: = "@@@Shashi";

String name3 = "@#$&shashi";

Output: name3: = "@#$&Shashi";

Note: Capitalize only first letter of alphabet, ignore space and special character.

2 Answers2

1

Try this to remove Special Characters

 public static String getOnlyStrings(String s) {
Pattern pattern = Pattern.compile("[^a-z A-Z]");
Matcher matcher = pattern.matcher(s);
String number = matcher.replaceAll("");
return number;

}

So your call should be

str = getOnlyStrings(str);

& then capitalized first letter using

str.replace(str.charAt(0),str.toUpperCase().charAt(0));

You may need to change the Pattern according to your needs, Current pattern only accepts Characters from a to z

Credits : Answer: How to remove special characters from a string?

Ganesh
  • 368
  • 5
  • 15
0
str.replaceAll(" ","");
str.replace(str.charAt(0),str.toUpperCase().charAt(0));

hm..... if you want to ignore the special character, recommend to use ASCIICODE and charAt method.

Yongho
  • 21
  • 5