Nafiz
Nafiz

Reputation: 206

Split every character of a word in android?

I am trying to Split every character of a word..Such as "CAT"=C,A,T

I have been able get the full length of the word..by taking input in a edit text..

package com.pack.name;

import android.R.array;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class NamtestActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    EditText name;
    Button save;
    String sname;
    int pname, i;
    char eachword[];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        name = (EditText) findViewById(R.id.editText1);
        save = (Button) findViewById(R.id.button1);
        save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        sname = name.getText().toString();
        pname = sname.length();
        Toast.makeText(getApplicationContext(), "" + pname, Toast.LENGTH_LONG)
                .show();
        //for (i = 0; i < pname; i++) {


        }

    }

}

Here we take input from the edittext and then by clicking the button it shows the length of the word ...Now what i need to split the word ..i was trying to do this in a for loop... Help Please .....

Upvotes: 0

Views: 3312

Answers (4)

K_Anas
K_Anas

Reputation: 31466

just call

"CAT".toCharArray();

you will get as the result

[C, A, T]

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

  1. You can do this using BreakIterator class with getCharaterInstance() static method.

    see this link for more detail:

    http://docs.oracle.com/javase/6/docs/api/java/text/BreakIterator.html

  2. You can manually do this using toCharArray().

    public void processSong(String word){

        //Conversion of String to Character Array//
    
        String s = word.toUpperCase();
        char[] tempArr = s.toCharArray();
        Character[] arr = new Character[tempArr.length];
    
    
    
    
        for (int i=0,j=0 ; i<tempArr.length ; i++,j++){
    
            arr[i] = tempArr[j];
        }
    
    
        for (Character c : l){
    
                 tempL.add(c);
    
    
    
        }
    
    
        Log.d("Vivek-Characters",tempL.toString());
        }
    

Upvotes: 4

Keppil
Keppil

Reputation: 46219

You can iterate through them like this:

for (char c: sname.toCharArray()) {
    ...
}

or just save them

char[] chars = sname.toCharArray();

Upvotes: 1

Yury
Yury

Reputation: 20936

You can simply use:

eachword = sname.toCharArray();

Upvotes: 4

Related Questions