---
description: This tutorial covers the split() string method in java definitions, How to split string in java with a delimiter, Split string with regex and length, and split with space.
title: Split() String Method in Java: How to Split String with Example
image: https://www.guru99.com/images/uploads/2012/08/SplitString.png
---

 

[Skip to content](#main) 

##  What is split() string in Java?

` StrSplit()` method allows you to break a string based on specific Java string delimiter. Mostly the Java string split attribute will be a space or a comma(,) with which you want to break or split the string

### split() function syntax

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

### Parameter

* Regex: The regular expression in Java split is applied to the text/string
* Limit: A limit in [Java string split](https://www.guru99.com/how-to-split-a-string-in-java.html) is a maximum number of values in the array. If it is omitted or zero, it will return all the strings matching a regex.

## How to Split a String in Java with Delimiter

Below example shows how to split string in Java with delimiter:

Suppose we have a string variable named `strMain` formed of a few words like Alpha, Beta, Gamma, Delta, Sigma – all separated by the comma (,).

[![ Split a String in Java ](https://www.guru99.com/images/uploads/2012/08/SplitString.png)](https://www.guru99.com/images/uploads/2012/08/SplitString.png)

Here if we want all individual strings, the best possible pattern would be to split it based on the comma. So we will get five separate strings as follows:

* Alpha
* Beta
* Gamma
* Delta
* Sigma

Use the string **split** in Java method against the string that needs to be divided and provide the separator as an argument.

In this Java split string by delimiter case, the separator is a comma (,) and the result of the Java split string by comma operation will give you an array split.

class StrSplit{
  public static void main(String []args){
   String strMain = "Alpha, Beta, Delta, Gamma, Sigma";
    String[] arrSplit = strMain.split(", ");
    for (int i=0; i < arrSplit.length; i++)
    {
      System.out.println(arrSplit[i]);
    }
  }
}

The loop in the code just prints each Java split string to array after the split function in Java, as shown below-

### Expected Output:

Alpha
Beta
Delta
Gamma
Sigma

## Example: Java String split() method with regex and length

Consider a situation, wherein you require only the first ‘n’ elements after the split function in Java but want the rest of the string to remain as it is. An output something like this-

1. Alpha
2. Beta
3. Delta, Gamma, Sigma

This can be achieved by passing another argument along with the split() string in Java operation, and that will be the limit of strings required.

**Consider the following code of split method in Java –**

class StrSplit2{
  public static void main(String []args){
   String strMain = "Alpha, Beta, Delta, Gamma, Sigma";
    String[] arrSplit_2 = strMain.split(", ", 3);
    for (int i=0; i < arrSplit_2.length; i++){
      System.out.println(arrSplit_2[i]);
    }
  }
}

### Expected Output:

Alpha
Beta
Delta, Gamma, Sigma

## How to Split a string in Java by Space

Consider a situation, wherein you want to split a string by space. Let’s consider an example here; we have a split string [Java variable](https://www.guru99.com/java-variables.html) named strMain formed of a few words **Welcome to Guru99.**

public class StrSplit3{  
public static void main(String args[]){  
String strMain ="Welcome to Guru99"; 
String[] arrSplit_3 = strMain.split("\\s");
    for (int i=0; i < arrSplit_3.length; i++){
      System.out.println(arrSplit_3[i]);
    }
  }
}

### Expected Output:

Welcome
to 
Guru99

This code is editable. Click Run to Compile + Execute   

![]()

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/split-string.png","url":"https://www.guru99.com/images/split-string.png","width":"400","height":"116","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/how-to-split-a-string-in-java.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/java-tutorials","name":"Java Tutorials"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/how-to-split-a-string-in-java.html","name":"Split() String Method in Java: How to Split String with Example"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/how-to-split-a-string-in-java.html#webpage","url":"https://www.guru99.com/how-to-split-a-string-in-java.html","name":"Split() String Method in Java: How to Split String with Example","dateModified":"2023-12-26T15:26:48+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/split-string.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/how-to-split-a-string-in-java.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"@type":"NewsArticle","headline":"Split() String Method in Java: How to Split String with Example","keywords":"java","dateModified":"2023-12-26T15:26:48+05:30","articleSection":"Java Tutorials","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"publisher":{"@id":"https://www.guru99.com/#organization"},"description":"This tutorial covers the split() string method in java definitions, How to split string in java with a delimiter, Split string with regex and length, and split with space.","copyrightYear":"2023","copyrightHolder":{"@id":"https://www.guru99.com/#organization"},"name":"Split() String Method in Java: How to Split String with Example","@id":"https://www.guru99.com/how-to-split-a-string-in-java.html#richSnippet","isPartOf":{"@id":"https://www.guru99.com/how-to-split-a-string-in-java.html#webpage"},"image":{"@id":"https://www.guru99.com/images/split-string.png"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/how-to-split-a-string-in-java.html#webpage"}}]}
```
