String Class

Split String in Java

Splits a string into parts based on a regex (usually a space) and stores it into an String array.


Syntax
String[] splitString = bigString.split(regex);

Notes

The regex is a string used to determine where to split the bigger string. For example, to extract individual words from a sentence, a space would be used as a regex.

An optional integer can be passed as a second parameter to limit the number of strings returned.


Example
String pets = "dog,cat";
String[] petsArray = pets.split(","); //will split names using comma as delimiter
System.out.println(petsArray[0]); //will output "dog"

Add to Slack

< String Trim   |   Substring >

© 2019 SyntaxDB. All Rights Reserved.