Skip to content

Instantly share code, notes, and snippets.

@akarnokd
Created June 15, 2022 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akarnokd/93b3be5822ac6a551460443aedf8b331 to your computer and use it in GitHub Desktop.
Save akarnokd/93b3be5822ac6a551460443aedf8b331 to your computer and use it in GitHub Desktop.

You'll have to escape the currency symbol as it might be a reserved symbol in regex. Plus, splitting doesn't keep the symbol itself so you'll have to add it back manually:

String currency = "$"; // from your method

String escapedCurrency = Pattern.quote(currency);

String[] result = "$5".split(escapedCurrency, 2);
result[0] = currency;

System.out.println(Arrays.toString(result));

Prints [$, 5]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment