'How to get an EXACT Character in a CharSequence?
I want to extract only the exact character in a CharSequence when I know the position (or key) of the character in it!
For eg: In the CharSequence "HELLO" I want to Extract the letter in the position 2 which is "E" in this case
. How can I do that?
TO BE MORE SPECIFIC: I am building an Android application where I am using the TextWatcher to obtain the text entered by people in the TextEdit field as follows.
EditText KeyLogEditText = (EditText) findViewById(R.id.editTextforKeyLog);
KeyLogEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Here is where I need help!!
// I have the "start" and "before" keys I have the position of the character inserted
// I need to obtain the EXACT character from the CharSequence "s", How do I Do that?
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
My Question is commented within the code as well.
Thanks for any help anyone can provide. Adit
Solution 1:[1]
Use the charAt()
method on CharSequence
.
Solution 2:[2]
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
// the EXACT character from the CharSequence "s"
int lastIndex = s.length() - 1;
char lastChar = s.charAt(lastIndex);
String lastStr = Character.toString(lastChar);
EditText.setText(lastStr);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CommonsWare |
Solution 2 | Oded Ben Dov |