Noor's Plugin

WordPress Plugins, Themes, Hosting, Tutorials & more!

  • Home
  • Plugins
    • Themes
  • Hosting
  • Contact
    • Privacy Policy
  • About

How to Convert a String to a Char Array in Java

A String variable in Java contains a collection of characters and is used for storing text. On the other hand, a variable of type Char is used for storing a single character. In this article, I will show you the easiest way to convert a String to a Char array in Java.

String to Char Array in Java

convert string to char array in java

Here is an example that will allow you to easily convert a String to a Char array in Java.

public class ConvertStringToCharArray {

	public static void main(String[] args) {
		String sampleString = "convert me to char";
		char[] charArray = sampleString.toCharArray();
 
		for (char ch : charArray) {
			System.out.println(ch);
		}
	}
}

How we are converting a String to a Char array:

  • First, we define a String variable named “sampleString” that contains this text – “convert me to char”.
  • Now we use the built-in toCharArray function to convert “sampleString” to a Char array. This value is stored into a new Char array defined as “charArray”.
  • We use a “For” loop to loop through each element of this “charArray” as “ch” and print the output on the screen.

This is how the output will look like:

c
o
n
v
e
r
t

m
e

t
o

c
h
a
r

Related

Written by Noor Alam · Categorized: blog

Leave a Reply Cancel reply

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

Search Noor’s Plugin

Recent Posts

  • How to Add a Quantity Field to a Stripe Payment Button
  • How to Add a Price Field to a PayPal Button
  • Hide Product Image for WooCommerce Plugin
  • How to Show a Download Button to Logged in Users Only
  • How to Add a Price Field to a Stripe Payment Button
  • How to Disable/Hide Featured Images in WordPress Astra Theme
  • What is the White Screen of Death in WordPress?
  • How to Fix YouTube Error The uploader has not made this video available in your country
  • How to Configure SendGrid SMTP in WordPress
  • Checkout for PayPal WordPress Plugin

Copyright © 2022 · Noor's Plugin

This website uses cookies to improve your experience. By continuing, you agree to their use.Got it! Learn More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT