Home > Software design >  How to print character, Number and special character from given String in Java?
How to print character, Number and special character from given String in Java?

Time:07-07

Given String is str = "aQt4512m@!%n" Output:

character:aQtmn , Number:4512 special:@!%

Below is the code, I have tried but, it's an only printing character

String str = "asDftQ32$34h";
        
char[] chArr = str.toCharArray();
Character myChar = '\0';
        
for(int i=0;i<chArr.length;i  ) {
    myChar = '\0';  
    if(chArr[i]>=65 && chArr[i]<=122) {
        myChar =chArr[i];   
    }
        
    String tr = myChar.toString();
    String dr = tr.replaceAll("\\W","");
    System.out.print(dr);
}

CodePudding user response:

You can do it using replaceAll() like in the exemple:

public class Main {
    public static void main( String[] arg ) {
        String str = "aQt4512m@!%n";

        String chars = str.replaceAll( "[\\W\\d]", "" );
        System.out.println( chars );

        String special = str.replaceAll( "\\w", "" );
        System.out.println( special );

        String numbers = str.replaceAll( "\\D", "" );
        System.out.println( numbers );
    }
}

Check this for more info on regular-expression: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

CodePudding user response:

 public void printString()
   {
       String input = "asDftQ32$34h";
       List<Character> numbers = new ArrayList<>();
       List<Character> letters = new ArrayList<>();
       List<Character> specialChars = new ArrayList<>();
       for (char current: input.toCharArray()) {
           if(Character.isDigit(current))
           {
               numbers.add(current);
           }
           else if(Character.isAlphabetic(current))
           {
               letters.add(current);
           }
           else
               specialChars.add(current);
       }
       System.out.println("All Letters In String :"  letters);
       System.out.println("All Numbers In String :"  numbers);
       System.out.println("All Special Characters In String :"  specialChars);


   }

Use the Character API

CodePudding user response:

Since you’re apparently allowed to use regular expressions, you can use character classes to make short work of this:

String str = "asDftQ32$34h";

System.out.println("character: "   str.replaceAll("\\P{L}", ""));
System.out.println("number: "   str.replaceAll("\\P{N}", ""));
System.out.println("special: "   str.replaceAll("\\p{Alnum}", ""));

CodePudding user response:

Here is code that:

  • processes a text string
  • inspects each character c along the way
  • if c is in the range of a-z, or A-Z, it appends to a StringBuilder named "characters"
  • if it's in the range of 0-9, it appends to "digits"
  • if it's in a few other ranges for special character things, it appends to "special"
  • if it's outside of any of those ranges, it prints a message along with the character
private static void separateCharacters(String str) {
    StringBuilder characters = new StringBuilder();
    StringBuilder digits = new StringBuilder();
    StringBuilder special = new StringBuilder();

    for (int i = 0; i < str.length(); i  ) {
        char c = str.charAt(i);
        if (Character.isAlphabetic(c)) {
            characters.append(c);
        } else if (Character.isDigit(c)) {
            digits.append(c);
        } else if ((c >= 33 && c <= 47) ||
                (c >= 58 && c <= 64) ||
                (c >= 91 && c <= 96) ||
                (c >= 123 && c <= 126)) {
            special.append(c);
        } else {
            System.out.println("skipping character: "   c);
        }
    }

    System.out.println("characters : "   characters);
    System.out.println("digits     : "   digits);
    System.out.println("special    : "   special);
}

Here is a run with output for "aQt4512m@!%n":

separateCharacters("aQt4512m@!%n");

characters : aQtmn
digits     : 4512
special    : @!%

Again, this time for "aQt4512m@!%n[-=":

separateCharacters("azrRt07572m@!%n[-=");

characters : azrRtmn
digits     : 07572
special    : @!%[-=

I don't love the "special" handling, but this should be enough of a working example to show how you could modify the code to include/exclude other characters for any of the different groups. Alternatively, you might choose to follow logic that all non-alpha, non-digit characters are "special", in which case you could make the default "else" statement be special.append(c).

  • Related