O.D
O.D

Reputation: 49

How to use split method in java the same as used in c#

I have this code which is written in c# however, I want to convert this code from c# to java, when I use split method in java it required a String parameter however in my case I have String[], so it cannot be applied, please tell me how to transfer these 2 line of codes, you assistance is highly appreciated.

string[] stringSeparators = new string[] { "3B3D3B" };
string[] separatedHex = returnHex.Split(stringSeparators, 
                   StringSplitOptions.RemoveEmptyEntries);

Upvotes: 0

Views: 221

Answers (2)

Martin Hlavňa
Martin Hlavňa

Reputation: 696

Looking at C# documentation it looks like Split(String[]) uses delimiter array as alternative delimiters.

As you have already noticed java does not have "split by array" method. But single string parameter in java is actually regular expression. Because of this you could join your your delimiters with alternative operator.

string[] stringSeparators = new string[] { "3B3D3B" };
string[] separatedHex = returnHex.split(String.join("|",stringSeparators));

But make sure your separator deos not contain any special regex characters - or make sure they are escaped with \

Upvotes: 2

Nehorai Elbaz
Nehorai Elbaz

Reputation: 2452

What about this code, let me know if this one works:

String stringSeparators = "3B3D3B";
String[] separatedHex = returnHex.split(stringSeparators);

Upvotes: 2

Related Questions