Adit Gupta
Adit Gupta

Reputation: 863

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

Upvotes: 3

Views: 2657

Answers (2)

developper-ta
developper-ta

Reputation: 31

@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);

}

Upvotes: -2

CommonsWare
CommonsWare

Reputation: 1006539

Use the charAt() method on CharSequence.

Upvotes: 8

Related Questions