# Splitting a string

**URL:** <https://forum.universal-robots.com/t/splitting-a-string/888>\
**Category:** Java\
**Created:** [June 15, 2017, 9:34am UTC](https://forum.universal-robots.com/t/splitting-a-string/888 "2017-06-15T09:34:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon82484413](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@anon82484413](https://forum.universal-robots.com/u/anon82484413)\
**Post date:** [June 15, 2017, 9:34am UTC](https://forum.universal-robots.com/t/splitting-a-string/888/1 "2017-06-15T09:34:13Z")

</div>

I got a string, which I am trying to split up.

The string looks like `50.5|70|10` and I am trying to get the different parts into different text fields.

```
		String[] slices = getSettings().split("|");
		part_diameterTextField.setText(slices[0]);
		part_lengthTextField.setText(slices[1]);
		pin_diameterTextField.setText(slices[2]);

```

I had expected this to put 50.5 into part\_diameter, 70 into part\_length, and 10 into pin\_diameter, but instead I get nothing in part\_diameter, 5 in part\_length, and 0 in pin\_diameter.

Am I doing the splitting wrong somehow?

---

<div class="post-metadata">

**Author:** ![anon82484413](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@anon82484413](https://forum.universal-robots.com/u/anon82484413)\
**Post date:** [June 15, 2017, 10:10am UTC](https://forum.universal-robots.com/t/splitting-a-string/888/2 "2017-06-15T10:10:17Z")

</div>

Ah… Apparently, using | does not work properly, but using ; works just fine.

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [June 15, 2017, 11:12am UTC](https://forum.universal-robots.com/t/splitting-a-string/888/3 "2017-06-15T11:12:41Z")

</div>

Thanks for the update, I am glad you resolved the issue.

---

<div class="post-metadata">

**Author:** ![mmi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mmi/32/691_2.png) [@mmi](https://forum.universal-robots.com/u/mmi)\
**Post date:** [June 20, 2017, 1:10pm UTC](https://forum.universal-robots.com/t/splitting-a-string/888/4 "2017-06-20T13:10:51Z")

</div>

Keep in mind that split() parameter is not just set of characters, it’s actually java regular expression. Some special characters have to be escaped to make it work. In this case you should use  
`String[] slices = getSettings().split("\\|");`

---

<div class="post-metadata">

**Author:** ![jkoripalli](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@jkoripalli](https://forum.universal-robots.com/u/jkoripalli)\
**Post date:** [May 12, 2021, 8:16pm UTC](https://forum.universal-robots.com/t/splitting-a-string/888/5 "2021-05-12T20:16:04Z")

</div>

Hello,

I am trying to achieve similar thing, but in script.

I have a string, for example:

Input=“5;3;2”

What should i do to separate them and equal to different variables?

for example:

a=Input[0]  
a=5  
b=Input[1]  
b=3

Thanks

---

<div class="post-metadata">

**Author:** ![terryc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/terryc/32/3006_2.png) [@terryc](https://forum.universal-robots.com/u/terryc)\
**Post date:** [May 13, 2021, 4:20am UTC](https://forum.universal-robots.com/t/splitting-a-string/888/6 "2021-05-13T04:20:32Z")

</div>

Check the script manual for str\_sub(). Returns substring of given length at given index.
