java - How to remove all number in string except "1" and "2"?

Java - How to remove all number in string except "1" and "2"?

To remove all numbers from a string except for the digits "1" and "2" in Java, you can use regular expressions (replaceAll() method) to achieve this. Here's how you can do it:

public class Main { public static void main(String[] args) { String input = "a1b2c3d4e5f"; // Remove all digits except "1" and "2" String result = input.replaceAll("[^12]", ""); System.out.println("Original string: " + input); System.out.println("Filtered string: " + result); } } 

Explanation:

  1. Regular Expression ([^12]):

    • [^12] matches any character that is not "1" or "2".
    • replaceAll("[^12]", "") replaces all characters that are not "1" or "2" with an empty string, effectively removing them from the original string.
  2. Example:

    • For the input string "a1b2c3d4e5f", the result of replaceAll("[^12]", "") will be "12".

Notes:

  • Regular Expression:

    • Adjust the regular expression [12] as needed if you have specific requirements (e.g., handling multiple occurrences, different patterns).
  • Character Class ([^12]):

    • [^12] is a negated character class that matches any character except those listed inside the square brackets (1 and 2 in this case).
  • Performance Considerations:

    • If performance is critical or if you're processing large strings, consider using StringBuilder for better efficiency.

This approach efficiently removes unwanted digits from a string while preserving the digits "1" and "2", using Java's String.replaceAll() method with a regular expression.

Examples

  1. Java remove all numbers except specific

    • Description: Removing all numbers from a string except '1' and '2' using regular expressions in Java.
    • Code Implementation:
      String input = "abc123def456ghi"; String result = input.replaceAll("[^12]", ""); System.out.println(result); // Output: "12" 
  2. Java remove digits except specified

    • Description: Using a custom method to filter out all digits except '1' and '2' from a string in Java.
    • Code Implementation:
      String input = "abc123def456ghi"; StringBuilder result = new StringBuilder(); for (char c : input.toCharArray()) { if (c == '1' || c == '2') { result.append(c); } } System.out.println(result.toString()); // Output: "12" 
  3. Java regex remove all numbers except certain

    • Description: Applying a regular expression to replace all digits except '1' and '2' from a string in Java.
    • Code Implementation:
      String input = "abc123def456ghi"; String result = input.replaceAll("[^12]", ""); System.out.println(result); // Output: "12" 
  4. Java filter string for specific numbers

    • Description: Filtering a string to include only characters '1' and '2' using Java streams.
    • Code Implementation:
      String input = "abc123def456ghi"; String result = input.chars() .filter(c -> c == '1' || c == '2') .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) .toString(); System.out.println(result); // Output: "12" 
  5. Java remove all digits except certain characters

    • Description: Using Apache Commons Lang library to remove all digits except specified characters ('1' and '2') in Java.
    • Code Implementation:
      import org.apache.commons.lang3.StringUtils; String input = "abc123def456ghi"; String result = StringUtils.removeAll(input, "[^12]"); System.out.println(result); // Output: "12" 
  6. Java regex exclude numbers except specific

    • Description: Using Java regex to exclude all digits except '1' and '2' from a string.
    • Code Implementation:
      import java.util.regex.Pattern; import java.util.regex.Matcher; String input = "abc123def456ghi"; Pattern pattern = Pattern.compile("[^12]"); Matcher matcher = pattern.matcher(input); String result = matcher.replaceAll(""); System.out.println(result); // Output: "12" 
  7. Java stream filter characters except specified

    • Description: Filtering characters from a string stream to include only '1' and '2' in Java.
    • Code Implementation:
      String input = "abc123def456ghi"; String result = input.chars() .filter(c -> c == '1' || c == '2') .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) .toString(); System.out.println(result); // Output: "12" 
  8. Java regex remove all numbers except specific

    • Description: Using regex to remove all numeric characters except '1' and '2' from a string in Java.
    • Code Implementation:
      String input = "abc123def456ghi"; String result = input.replaceAll("[^12]", ""); System.out.println(result); // Output: "12" 
  9. Java remove digits but keep specific

    • Description: Removing all digits from a string while preserving '1' and '2' using Java streams.
    • Code Implementation:
      String input = "abc123def456ghi"; String result = input.replaceAll("[^12]", ""); System.out.println(result); // Output: "12" 
  10. Java regex replace all except specified

    • Description: Using regex to replace all characters in a string except '1' and '2' with an empty string in Java.
    • Code Implementation:
      String input = "abc123def456ghi"; String result = input.replaceAll("[^12]", ""); System.out.println(result); // Output: "12" 

More Tags

dummy-variable azure mailmessage sidenav accelerometer monodevelop wpf-positioning google-pay ado.net histogram

More Programming Questions

More Pregnancy Calculators

More Weather Calculators

More Electronics Circuits Calculators

More General chemistry Calculators