Cannot understand why when user input bachelor format like "B.Com"
.
I want to get "B.com"
- c
letter is lower case.
But I'm getting: "B.Com"
- C
letter is capital
Here is my code:
InputFilter value = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned spanned, int dstart, int dend) {
for(int i = start; i < end; i++) {
String letterOne = source.subSequence(0,1).toString().toUpperCase();
if(Character.toString(source.charAt(i)).matches("[a-zA-Z.? ]*")) {
return source.subSequence(0,1).toString().toUpperCase()+source.subSequence(1,end).toString().toLowerCase();
}
return letterOne+source.subSequence(1,end).toString().toLowerCase();
}
return null;
}
};
your current code in
for
statement may be reducedinside
if
statement you havesource.subSequence(0,1).toString().toUpperCase()
, which is exacly same asletterOne
, so we can reduce some codenow it is clearly visible that
if
statement contains samereturn
as lastreturn
in method, so it may be reduced again to:in above code you are not using
i
at all, so wholefor
statement is useless andfilter
may by shorted to:there is no
.
-present checking code, no uppercasing any other letter than first... you are always returning sameString
assource
with first letter uppercased and all other lowercasedyou may try to use indexOf method for checking if dot is present in whole
String sourceAsString
(convert fromCharSequence source
), if yes then use split method for making array with twoString
s - make first letter uppercase in both words and join themString properlyFormatted = firstWord + "." + secondWord;
PS. be aware that
split
method takes REGEX as param and"."
in REGEX means "any letter", so use escaping like this:split("\\.")