Languages
[Edit]
EN

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

0 points
Created by:
Dirask Admin - Marwin
1430

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 the 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 );
    }
}

Output: 

split
this
text
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.

❤️💻 🙂

Join