S
S
steve_fahrenheit2020-05-27 17:38:30
Java
steve_fahrenheit, 2020-05-27 17:38:30

How to pull out characters other than comma and numbers in Java?

Hello, please help

How can I extract characters other than commas and numbers in Java?
* in android studio

Let's say I have:
100 ₽
$4.99
9.99 €
100 CA$
9.49 USD

It is necessary that the letters also be

How can I extract their currency signs from them (not all at once) (the Canadian dollar has them together with CA)
I understand, I need to filter something so that everything is selected, except for numbers and commas

. Please tell me how to do it :) Thank you :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2020-05-27
@steve_fahrenheit

public class Main {

    public static void main(String[] args) {

        String a = "100 ₽";
        String b = "$4,99";
        String c = "9,99 €";
        String d = "100 CA$";

        // Паттерн
        String regex = "\\p{Sc}";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

        // передали переменную a, b, c или d в метод matcher()
        Matcher matcher = pattern.matcher(a);

        while (matcher.find()) {
            // Выведет ₽, $, €, $
            System.out.println(matcher.group());
        }

    }

}

https://howtodoinjava.com/regex/java-regex-match-a...

B
Billy Milligan, 2020-05-28
@Billy_Milligan

var prices = new String[] {
    "100 ₽",
    "$4,99",
    "9,99 €",
    "100 CA$",
    "9,49 USD"
};

//Паттерн
final var pattern = "[\\d\\s.,]+";

var resultSet = Arrays.stream(prices)
        .map(price -> price.replaceFirst(pattern, ""))//Вырезаем все совпадения
        .collect(Collectors.toSet());//собираем в Set

resultSet.forEach(System.out::println);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question