1.

What is the best way to split a string in Java?

Answer»

Split() is a Java method for breaking a string based on a Java string delimiter (specified REGEX). For example, a space or a comma(,) will usually be used as the Java string split attribute to break or split the string.

Syntax: 

string.split(String regex, INT limit)

Here,

  • regex: String is DIVIDED at this specified regex.
  • limit (optional parameter): Controls or limits the number of resulting substrings. Split() returns all potential substrings if the limit parameter is not specified or is 0.

Example:

public class SplitString { public static void main(String[] ARGS) { String str = "Scaler by InterviewBit"; // split string from space String[] result = str.split(" "); for (int i=0; i < result.length; i++) { System.out.println(result[i]); } }}

Output: 

ScalerbyInterviewBit


Discussion

No Comment Found