logo

سلسلة جافا toCharArray ()

ال سلسلة جافا toCharArray () تقوم الطريقة بتحويل هذه السلسلة إلى مجموعة أحرف. تقوم بإرجاع مصفوفة أحرف تم إنشاؤها حديثًا، وطولها مشابه لهذه السلسلة وتتم تهيئة محتوياتها باستخدام أحرف هذه السلسلة.


التنفيذ الداخلي

 public char[] toCharArray() { // Cannot use Arrays.copyOf because of class initialization order issues char result[] = new char[value.length]; System.arraycopy(value, 0, result, 0, value.length); return result; } 

إمضاء

التوقيع أو بناء جملة طريقة toCharArray() موضح أدناه:

 public char[] toCharArray() 

عائدات

مصفوفة الأحرف


مثال على طريقة Java String toCharArray()

 public class StringToCharArrayExample{ public static void main(String args[]){ String s1=&apos;hello&apos;; char[] ch=s1.toCharArray(); for(int i=0;i<ch.length;i++){ system.out.print(ch[i]); } }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> hello </pre> <h2>Java String toCharArray() Method Example 2</h2> <p>Let&apos;s see one more example of char array. It is useful method which returns char array from the string without writing any custom code. </p> <pre> public class StringToCharArrayExample2 { public static void main(String[] args) { String s1 = &apos;Welcome to Javatpoint&apos;; char[] ch = s1.toCharArray(); int len = ch.length; System.out.println(&apos;Char Array length: &apos; + len); System.out.println(&apos;Char Array elements: &apos;); for (int i = 0; i <len; i++) { system.out.println(ch[i]); } }< pre> <p>Output:</p> <pre> Char Array length: 21 Char Array elements: W e l c o m e t o J a v a t p o i n t </pre></len;></pre></ch.length;i++){>

Java String toCharArray() طريقة المثال 2

دعونا نرى مثالاً آخر لمصفوفة char. إنها طريقة مفيدة تقوم بإرجاع مصفوفة الأحرف من السلسلة دون كتابة أي تعليمات برمجية مخصصة.

اجتياز الشجرة مسبقًا
 public class StringToCharArrayExample2 { public static void main(String[] args) { String s1 = &apos;Welcome to Javatpoint&apos;; char[] ch = s1.toCharArray(); int len = ch.length; System.out.println(&apos;Char Array length: &apos; + len); System.out.println(&apos;Char Array elements: &apos;); for (int i = 0; i <len; i++) { system.out.println(ch[i]); } }< pre> <p>Output:</p> <pre> Char Array length: 21 Char Array elements: W e l c o m e t o J a v a t p o i n t </pre></len;>