简体   繁体   中英

Adding special characters to an array

I have a function that adds capitals, lowercase, and 0-9 to a character array and now i want to add special characters such as !@#$%^&*() . The format for my array goes like this:

 for (char ch = '0'; ch <= '9'; ++ch)
          tmp.append(ch);
        for (char ch = 'a'; ch <= 'z'; ++ch)
          tmp.append(ch);
        for (char ch = 'A'; ch <= 'Z'; ++ch)
          tmp.append(ch);


        symbols = tmp.toString().toCharArray();

How do I add the special characters, using the same format if possible, without adding them one by one?

I agree with the comment Elliott made. you can add the characters !"#$%&'()*+,-./ by using

for(int i = 33 ; i < 48; i++)
    tmp.append(Character.toChars(i))

The ASCII values of the characters which I listed is from 33 to 48, they will be converted to the keyboard characters and can be appended.

如果tmp是StringBuilder或StringBuffer,则可以在一个方法调用中附加所有字符:

tmp.append( "!@#$%^&*()" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM