Convert Java char Array To String

This example will show you three methods that can convert a Java char array to a Java String object. Each method has it’s unique advantage and disadvantages. Please see the code comments for detail introduction.

public class ConvertCharArrayToString {

	public static void main(String[] args) {
		
		/* Declare an array. */
		char chaArrObj[] = {'i',' ','l','o','v','e',' ','j','a','v','a'};
		
		ConvertCharArrayToString.charArrryToString1(chaArrObj);
		
		ConvertCharArrayToString.charArrryToString2(chaArrObj);
		
		ConvertCharArrayToString.charArrryToString3(chaArrObj);

	}
	
	/* Use constructor to create directly from the array.*/
	public static String charArrryToString1(char chaArr[])
	{
		String retStr = new String(chaArr);
		System.out.println(retStr);
		return retStr;
	}
	
	/* Use StringBuffer. */
	public static String charArrryToString2(char chaArr[])
	{
		/* Declare a StringBuffer obect.*/
		StringBuffer strBuf = new StringBuffer();
		
		if(chaArr!=null)
		{
			/* Loop in the array, append each char to the StringBuffer object. 
                           Because only one StringBuffer object is created, so this method will cost less. */
			int len = chaArr.length;
			for(int i=0;i<len;i++)
			{
				char cha = chaArr[i];
				strBuf.append(cha);
			}
		}
		
		/* Translate strBuf object to retStrObj.*/
		String retStrObj = strBuf.toString();
		System.out.println(retStrObj);
		
		return retStrObj;
	}

	/* Use "+" operator. */
	public static String charArrryToString3(char chaArr[])
	{
		/* Declare return variable.*/
		String ret = "";
		if(chaArr!=null)
		{
			/* Loop in the array. */
			int len = chaArr.length;
			for(int i=0;i<len;i++)
			{
				/* Concat the char to the return object, each operation will crate a new String object. */
				char cha = chaArr[i];
                                /* Because each "+" action will create a new object, so this method will cost more memory and time. */
				ret += cha;
			}
		}
		
		System.out.println(ret);
		
		return ret;
	}
}

Download “ConvertCharArrayToString.zip” ConvertCharArrayToString.zip – Downloaded 62 times – 764 B

(Visited 124 times, 1 visits today)
READ :   How To Auto Import All Class In Android Studio

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.