Anand
Anand

Reputation: 21358

Splitting the string in Java to give the desired output

I have a String like "key=value==". I want to split the above to give me the output as

key  
value==

When i used ("key=value==").split("="), it gave me the below

key
value

I understand why it is giving this(because of '=' in value==).May I know how to correct it? How should i write my Java code so that I can get below as output

key  
value==

Upvotes: 1

Views: 92

Answers (3)

Benjamin Kalloch
Benjamin Kalloch

Reputation: 178

Using a regular expression as the split-statement is also possible:

("key=value==").split("\\=(?!=)(?!$)");

This expression will match any '=' if it is not followed by another '=' or if it is not the last one in the current line.

Upvotes: 0

Max Meijer
Max Meijer

Reputation: 1729

You can specify a limit for the split function. The second parameter is the maximum number of elements you want to get. In your case that is 2.

("key=value==").split("=",2)

Upvotes: 4

Anand
Anand

Reputation: 21358

After some tries, I got the solution as below

("key=value==").split("=",2);

Upvotes: 1

Related Questions