Saranya
Saranya

Reputation: 65

C# Equivalent java code

What is the equivalent for this C# code in Java?

string receivedData = …;
string splittedValues = receivedData.Split("&", StringSplitOptions.RemoveEmptyEntries);

Upvotes: 1

Views: 295

Answers (3)

Marko Topolnik
Marko Topolnik

Reputation: 200216

final String[] splittedValues = receivedData.replaceFirst("^&+","").split("&+");

Upvotes: 6

Louis Wasserman
Louis Wasserman

Reputation: 198321

With Guava:

Iterable<String> splitStrings = 
  Splitter.on('&').omitEmptyStrings().split(string);

(Disclosure: I contribute to Guava.)

Upvotes: 0

nhahtdh
nhahtdh

Reputation: 56819

For the particular code above, you can first:

  • Remove leading/trailing tokens of the delimiter: .replaceAll("(^&+|&+$)", "")
  • Split the string according to the delimiter: .split("&+")

Without the first step clean up, empty string will be in the result of splitting the string "&&sdfds" (leading delimiter).

Upvotes: 0

Related Questions