
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy Characters from String into Char Array in Java
Let’s say we have the following string.
String str = "Demo Text!";
To copy some part of the above string, use the getChars() method.
// copy characters from string into chArray =str.getChars( 0, 5, chArray, 0 );
The following is an example to copy characters from string into char Array in Java.
Example
public class Demo { public static void main(String[] args) { String str = "Demo Text!"; char chArray[] = new char[ 5 ]; System.out.println("String: "+str); // copy characters from string into chArray str.getChars( 0, 5, chArray, 0 ); System.out.print("Resultant character array...\n"); for (char ch : chArray) System.out.print( ch ); } }
Output
String: Demo Text! Resultant character array... Demo
Advertisements