Tutorials  /  JavaScript

String to Char Conversion in Java

Ccentron Redaktion · January 2024 ·2 min read ·JavaScript, Tutorial

Sometimes we have to convert String to the character array in java programs or convert a string to char from specific index.

String to char Java

String class has three methods related to char. Let’s look at them before we look at a java program to convert string to char array.

  • char[] toCharArray(): This method converts string to character array. The char array size is same as the length of the string.
  • char charAt(int index): This method returns character at specific index of string. This method throws StringIndexOutOfBoundsException if the index argument value is negative or greater than the length of the string.
  • getChars(int srcBegin, int srcEnd, char dst[], int dstBegin): This is a very useful method when you want to convert part of string to character array. First two parameters define the start and end index of the string; the last character to be copied is at index srcEnd-1. The characters are copied into the char array starting at index dstBegin and ending at dstBegin + (srcEnd-srcBegin) - 1.

Let’s look at a simple string to char array java program example.

Go
package com.journaldev.string;

public class StringToCharJava {

    public static void main(String[] args) {
        String str = "journaldev";
        
        //string to char array
        char[] chars = str.toCharArray();
        System.out.println(chars.length);
        
        //char at specific index
        char c = str.charAt(2);
        System.out.println(c);
        
        //Copy string characters to char array
        char[] chars1 = new char[7];
        str.getChars(0, 7, chars1, 0);
        System.out.println(chars1);
        
    }

}
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Consclusion

In above program, toCharArray and charAt usage is very simple and clear. In getChars example, first 7 characters of str will be copied to chars1 starting from its index 0. That’s all for converting string into char java program.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren