BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Split String into array of Characters in Java

By Chaitanya Singh | Filed Under: Java Examples

In this guide, we will see how to split a string into array of characters in Java. This can be archived by using this regex (?!^) in the split method of Java string class.

Java Program to Split String into Array of Characters

Explanation of regex ( ? ! ^ ):

The ?! part in this regex is negative assertion, which it works like a not operator in the context of regular expression. The ^ is to match the beginning of the string. Together it matches any character that is not the beginning of the string, which means it splits the string on every character.

public class JavaExample
{
  public static void main (String[] args)
  {
    String str = "BeginnersBook.com";

    //This will split a string into individual characters
    String[] strArray = str.split("(?!^)");

    //print array of characters
    for(String s: strArray){
      System.out.println(s);
    }
  }
}

Output:

Split String into array of characters in java example output

Related guides:

  • Split string by multiple delimiters
  • Split string by dot in java
  • Split string by space
  • Split string by pipe character

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap