Java - split string by hyphen sign (minus sign - ascii 45 code)

0 points
Created by:

In this article, we would like to show you how to split string by minus sign in Java.

String split() function takes as parameter character by which the string will be split into an array of substrings. As a result of split(), we get an array. To access an element we just get an element by its index.

Practical example

public class StringExample {

    public static void main(String[] args) {
        String text = "split-this-text";
        String[] split = text.split("-");

        String element1 = split[0]; // split
        String element2 = split[1]; // this
        String element3 = split[2]; // text

        System.out.println( element1 );
        System.out.println( element2 );
        System.out.println( element3 );



Alternative titles

  1. Java - how to split string by hyphen sign (minus sign - ascii 45 code)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - String (popular problems)

Java - split string by hyphen sign
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂