Java lang string split

Java.lang.String.split() Method

The java.lang.String.split(String regex, int limit) method splits this string around matches of the given regular expression.

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.

If the expression does not match any part of the input then the resulting array has just one element, namely this string.

Declaration

Following is the declaration for java.lang.String.split() method

public String[] split(String regex, int limit)

Parameters

  • regex − This is the delimiting regular expression.
  • limit − This controls the number of times the pattern is applied and therefore affects the length of the resulting array

Return Value

This method returns the array of strings computed by splitting this string around matches of the given regular expression.

Exception

PatternSyntaxException − if the regular expression’s syntax is invalid.

Example

The following example shows the usage of java.lang.String.split() method.

package com.tutorialspoint; import java.lang.*; public class StringDemo < public static void main(String[] args) < String str = "a d, m, i.n"; String delimiters = "\\s+|,\\s*|\\.\\s*"; // analyzing the string String[] tokensVal = str.split(delimiters); // prints the count of tokens System.out.println("Count of tokens = " + tokensVal.length); for(String token : tokensVal) < System.out.print(token); >// analyzing the string with limit as 3 tokensVal = str.split(delimiters, 3); // prints the count of tokens System.out.println("\nCount of tokens result notranslate"> Count of tokens = 5 admin Count of tokens = 3 adm, i.n

Источник

Читайте также:  Form input type file javascript
Оцените статью