Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8347027

String replaceAll with Function<String, String>

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Please add replaceAll(String regex, Function<String, String> replacer) method to java.lang.String class, example of use:
      ```java
      String test = "Text with русским text";
      System.out.println(test.replaceAll("([а-я]+)", matcher -> matcher.group().toLowerCase())); // Output: Text with РУССКИМ text
      System.out.println(test.replaceAll("([а-яА-Я]+)", matcher -> "")); // Output: Text with text
      String nums = "num1 число2 num3";
      System.out.println(replaceAll(nums, "[\\w\\p{IsCyrillic}]+\\d", m -> {
      boolean isRussian = m.matches(".*[а-яА-Я].*");
      return isRussian ? "<РУС>" : "<ENG>";
      })); // Output: "<ENG> <РУС> <ENG>"
      ```
      Code of method:
      ```java
      public static String replaceAll(String regex, Function<String, String> replacer) {
      return Pattern.compile(regex).matcher(this).replaceAll(r -> replacer.apply(r.group()));
      }
      ```


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: