java - Looking for method to remove spaces on sides, change all letters to small with first letter as capital letter -
i have been trying while make method takes user input , changes potential spaces infront , after text should removed. tried .trim() doesnt seem work on input strings 2 words. didnt manage make both first , second word have first letter capital.
if user inputs following string want separate words have small letters except first in word. e.g: long jump if user inputs:
"long jump"
or
" long jump "
change to
"long jump"
private string normalisera(string s) { return s.trim().substring(0,1).touppercase() + s.substring(1).tolowercase(); }
i tried method above didnt work 2 words, if input one. should work both
to remove spaces spaces can this
string = string.trim().replaceall(" +", " ");
the above code call trim rid of spaces @ start , end, use regex replace has 2 or more spaces single space.
to capitalize first word, if you're using apache's commons-lang
, can use wordutils.capitalizefully
. otherwise, you'll need use homebrewed solution.
simply iterate through string, , if current character space, mark next character uppercased. otherwise, make lowercase.
Comments
Post a Comment