Saurabh Kumar
Saurabh Kumar

Reputation: 16651

Removing leading spaces from a string

I have a String like follows

 abc, def tgy, awed djdj

Now i want to get all the parts of this string seperated by comma. Best way i can image is doing the following

final String[] splits = country.split(",");

Now i should have a array with 3 strings. But i dont want the leading spaces in each string also. How can i remove them. Is there a special way to do that? Or i have to use the same old regex way to remove the leading spaces.

Upvotes: 1

Views: 133

Answers (3)

vks
vks

Reputation: 67968

,\s*

You can split by this as well.This will remove leading spaces.See demo.

http://regex101.com/r/jT3pG3/13

Upvotes: 0

ThMBc
ThMBc

Reputation: 794

use this call on a string to remove leading and trailing spaces:

arbitraryVariable = arbitraryVariable.trim()

Upvotes: 1

anubhava
anubhava

Reputation: 784968

You can use this regex to remove leading/trailing spaces from split elements:

final String[] splits = country.trim().split(" *, *");

Upvotes: 3

Related Questions