user3861559
user3861559

Reputation: 993

Java Split function upon function returning value

I am making a function call and using split on the return value.

The function call is

<c:set var="locale" value="<%= request.getHeader("Accept-Language").split(",")[0] %>"/>

request.getHeader returns this

en,en-US;q=0.8

I want to split it in a way the variable locale has only

en-US

I tried multiple things but wasnt able figure out. The closest I got was splitting by "0" that gives me "en"

Upvotes: 1

Views: 42

Answers (1)

exception1
exception1

Reputation: 1249

You can use regular expressions to define more than one character as delimiter:

    String test = "en,en-US;q=0.8";
    String[] tokens = test.split("[,;]");
    System.out.println(tokens[1]);

prints

en-US

The brackets can be interpreted as "one of". The string is split by one of the characters , or ;.

Upvotes: 4

Related Questions