eggHunter
eggHunter

Reputation: 389

Splitting a string in Java after adding a delimiter

I've written some code which utilizes the split() method to return the first item delimited by periods. After a little testing I found that the array I split the string into has a length of 0 so I assume it's not splitting at all. It may be relevant that in some cases there is no period and I want the entire string returned. To compensate for this, I added a period onto the end of each String. See below:

longText=longText+".";
String tempName[]=longText.split(".");
String realName=tempName[0];
System.out.println(realName); 
return realName;

Upvotes: 1

Views: 517

Answers (1)

reto
reto

Reputation: 10453

The method String#split takes a regular expression as an argument. See Java Doc

The following: Split String on dot . as delimiter will help you

Upvotes: 1

Related Questions