Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Java String to ascii

Tags:

I need to convert Strings that consists of some letters specific to certain languages (like HÄSTDJUR - note Ä) to a String without those special letters (in this case HASTDJUR). How can I do it in Java? Thanks for help!


It is not really about how it sounds. The scenario is following - you want to use the application, but don't have the Swedish keyboard. So instead of looking at the character map, you type it by replacing special letters with the typical letters from the latin alphabet.

like image 993
grem Avatar asked Sep 14 '10 10:09

grem


People also ask

How do I encode a Java string as ASCII?

Convert String to ASCII 2.1 We can use String. getBytes(StandardCharsets. US_ASCII) to convert the string into a byte arrays byte[] , and upcast the byte to int to get the ASCII value. 2.2 Java 9, there is a new API String.

Are Strings ASCII in Java?

Java uses a multibyte encoding of Unicode characters. The Unicode character set is a super set of ASCII. So there can be characters in a Java string that do not belong to ASCII.

How do I get the Ascii code for a string?

ASCII value of a String is the Sum of ASCII values of all characters of a String. Step 1: Loop through all characters of a string using a FOR loop or any other loop. Step 3: Now add all the ASCII values of all characters to get the final ASCII value. A full example program is given below.

Can we convert string to char in Java?

We can convert String to char in java using charAt() method of String class. The charAt() method returns a single character only. To get all characters, you can use loop.


2 Answers

I think your question is the same as this one:

Java - getting rid of accents and converting them to regular letters

and hence the answer is also the same:

Solution

String convertedString =         Normalizer            .normalize(input, Normalizer.Form.NFD)            .replaceAll("[^\\p{ASCII}]", ""); 

References

See

  • JavaDoc: Normalizer.normalize(String, Normalizer.Form)
  • JavaDoc: Normalizer.Form.NFD
  • Sun Java Tutorial: Normalizer's API

Example Code:

final String input = "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ"; System.out.println(     Normalizer         .normalize(input, Normalizer.Form.NFD)         .replaceAll("[^\\p{ASCII}]", "") ); 

Output:

This is a funky String

like image 101
Sean Patrick Floyd Avatar answered Oct 12 '22 23:10

Sean Patrick Floyd


I'd suggest a mapping, of special characters, to the ones you want.

Ä --> A é --> e A --> A (exactly the same) etc... 

And then you can just call your mapping over your text (in pseudocode):

for letter in string:    newString += map(letter) 

Effectively, you need to create a set of rules for what character maps to the ASCII equivalent.

like image 36
Noel M Avatar answered Oct 12 '22 22:10

Noel M