Java Online Compiler
Example: Get String Length by Converting to Character Array in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Get String Length by Converting to Character Array import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Initialize a string String str = "Programming"; // Step 2: Convert the string to a character array char[] charArray = str.toCharArray(); // Step 3: Access the 'length' property of the character array int length = charArray.length; System.out.println("String: \"" + str + "\""); System.out.println("Length (using toCharArray().length): " + length); } }
Output
Clear
ADVERTISEMENTS