# String.split() java function help

**URL:** <https://discuss.codechef.com/t/string-split-java-function-help/74420>\
**Category:** Uncategorized\
**Tags:** help, java\
**Created:** [August 12, 2020, 8:37am UTC](https://discuss.codechef.com/t/string-split-java-function-help/74420 "2020-08-12T08:37:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![coderwhogive69](https://discuss.codechef.com/user_avatar/discuss.codechef.com/coderwhogive69/32/24644_2.png) [@coderwhogive69](https://discuss.codechef.com/u/coderwhogive69)\
**Post date:** [August 12, 2020, 8:37am UTC](https://discuss.codechef.com/t/string-split-java-function-help/74420/1 "2020-08-12T08:37:22Z")

</div>

String s=“0.123”;  
String x[]=s.split(".");  
out.printLine(x[0]+" "+x[1]);

This is giving an ArrayIndexOutOfBounds Exception. Can someone please help???

---

<div class="post-metadata">

**Author:** ![ssjgz](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ssjgz/32/346011_2.png) [@ssjgz](https://discuss.codechef.com/u/ssjgz)\
**Post date:** [August 12, 2020, 8:46am UTC](https://discuss.codechef.com/t/string-split-java-function-help/74420/2 "2020-08-12T08:46:21Z")

</div>

Your code has several compiler errors - please post your _full_, [formatted](https://discuss.codechef.com/t/tutorial-color-format-the-code/36585) code!

Note that `String.split` takes a regex, so you probably meant something like this:

```java
import java.io.*;

class Main{
	public static void main(String[] args) throws IOException{
        String s="0.123";
        String x[]=s.split("\\.");
        System.out.println(x[0]+" "+x[1]);
	}
}

```

---

<div class="post-metadata">

**Author:** ![coderwhogive69](https://discuss.codechef.com/user_avatar/discuss.codechef.com/coderwhogive69/32/24644_2.png) [@coderwhogive69](https://discuss.codechef.com/u/coderwhogive69)\
**Post date:** [August 12, 2020, 9:29am UTC](https://discuss.codechef.com/t/string-split-java-function-help/74420/3 "2020-08-12T09:29:35Z")

</div>

I just skipped the main function to make it short but thanks. What does a regex mean??  
Like the split function does work for .split(" “) and .split(”,") without any exception.

---

<div class="post-metadata">

**Author:** ![ssjgz](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ssjgz/32/346011_2.png) [@ssjgz](https://discuss.codechef.com/u/ssjgz)\
**Post date:** [August 12, 2020, 9:30am UTC](https://discuss.codechef.com/t/string-split-java-function-help/74420/4 "2020-08-12T09:30:58Z")

</div>

> **[Regular expression](https://en.wikipedia.org/wiki/Regular_expression)**
>
> A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. Regular expression techniques are developed in theoretical computer science and formal language theory.
> The concept of regular expressions began in the 1950s, when the American mathematician ...

---

<div class="post-metadata">

**Author:** ![coderwhogive69](https://discuss.codechef.com/user_avatar/discuss.codechef.com/coderwhogive69/32/24644_2.png) [@coderwhogive69](https://discuss.codechef.com/u/coderwhogive69)\
**Post date:** [August 12, 2020, 9:40am UTC](https://discuss.codechef.com/t/string-split-java-function-help/74420/5 "2020-08-12T09:40:50Z")

</div>

Why does the function work just fine with .split(" “) and .split(”,") I mean it gives the correct output with “,” instead of “\,”

---

<div class="post-metadata">

**Author:** ![ssjgz](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ssjgz/32/346011_2.png) [@ssjgz](https://discuss.codechef.com/u/ssjgz)\
**Post date:** [August 12, 2020, 9:43am UTC](https://discuss.codechef.com/t/string-split-java-function-help/74420/6 "2020-08-12T09:43:23Z")

</div>

`,` and “space”

have no special meaning in regexes;

`.`

does, so needs to be escaped.

---

<div class="post-metadata">

**Author:** ![coderwhogive69](https://discuss.codechef.com/user_avatar/discuss.codechef.com/coderwhogive69/32/24644_2.png) [@coderwhogive69](https://discuss.codechef.com/u/coderwhogive69)\
**Post date:** [August 12, 2020, 9:47am UTC](https://discuss.codechef.com/t/string-split-java-function-help/74420/7 "2020-08-12T09:47:06Z")

</div>

Thanks for your help. I spent a lot of time before this on the net to find the mistake but couldn’t find one. Thanks once again.
