Sunday, October 11, 2015

java - How to convert a char to a string

MainActivity.java

package com.cfsuman.me.javaexamples;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set background color for action bar
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));

        // Get widget reference from XML layout
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        TextView tv = (TextView) findViewById(R.id.tv);

        // Empty the TextView
        tv.setText("");

        char charValueA = 'A';
        // Convert the char to a String
        String stringOfCharValueA = ""+charValueA;

        // Display the conversion result in TextView
        tv.setText(tv.getText() + "Char : " + charValueA);
        tv.setText(tv.getText() + "\nType: " + stringOfCharValueA.getClass().getName());
        tv.setText(tv.getText() + "\nString: " + stringOfCharValueA);

        char charValueB = 'B';
        // Another way to convert a char to String
        // This is suggested way to convert
        String stringOfCharValueB = String.valueOf(charValueB);

        // Display the conversion result in TextView
        tv.setText(tv.getText() + "\n\nChar : " + charValueB);
        tv.setText(tv.getText() + "\nType: " + stringOfCharValueB.getClass().getName());
        tv.setText(tv.getText() + "\nString: " + stringOfCharValueB);

        char charValueC = 'C';
        // Another way to convert a char to String
        String stringOfCharValueC = Character.toString(charValueC);

        // Display the conversion result in TextView
        tv.setText(tv.getText() + "\n\nChar : " + charValueC);
        tv.setText(tv.getText() + "\nType: " + stringOfCharValueC.getClass().getName());
        tv.setText(tv.getText() + "\nString: " + stringOfCharValueC);

        char charValueD = 'D';
        // Another way to convert a char to String
        String stringOfCharValueD = new Character(charValueD).toString();

        // Display the conversion result in TextView
        tv.setText(tv.getText() + "\n\nChar : " + charValueD);
        tv.setText(tv.getText() + "\nType: " + stringOfCharValueD.getClass().getName());
        tv.setText(tv.getText() + "\nString: " + stringOfCharValueD);

        char charValueE = 'E';
        // Another way to convert a char to String
        String stringOfCharValueE = new String(new char[charValueE]);

        // Display the conversion result in TextView
        tv.setText(tv.getText() + "\n\nChar : " + charValueE);
        tv.setText(tv.getText() + "\nType: " + stringOfCharValueE.getClass().getName());
        tv.setText(tv.getText() + "\nString: " + stringOfCharValueE);

        char charValueF = 'F';
        // Another way to convert a char to String
        String stringOfCharValueF = String.valueOf(new char[]{charValueF});

        // Display the conversion result in TextView
        tv.setText(tv.getText() + "\n\nChar : " + charValueF);
        tv.setText(tv.getText() + "\nType: " + stringOfCharValueF.getClass().getName());
        tv.setText(tv.getText() + "\nString: " + stringOfCharValueF);
   }
}
More android examples