Java Char Array to String

Java’da char array’i nasıl string yapılır? Cevabı:

import java.util.*;
import java.lang.*;

public class Main
{
public static void main (String[] args) throws java.lang.Exception
{ char[] a = {‘m’,’e’,’r’,’h’,’a’,’b’,’a’,”};

//Birinci yol diyelim
String s=new String(a);
System.out.println(s);

//Normal yazdırınca bu çıkar zaten
System.out.println(a);

//StringBuilder ile yapalım
StringBuilder sb=new StringBuilder();
sb.append(a);
String diff=sb.toString();
System.out.println(diff);

//Birkaç farklı yöntem daha…
String text = String.copyValueOf(a);
String text2 = String.valueOf(a);
System.out.println(text);
System.out.println(text2);
return;
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s