1

My java program gets some weather information from an API. But it has weird letters in the text. Looks like ASCII code.

Here is an example: Min temp: 0°C (32°F) which should be: Min temp: 0C (32F) (i think).

How can I change it?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Alex Jj
  • 1,343
  • 10
  • 19
  • 30

4 Answers4

1

Well one solution can be before posting you can do following

String withoutDegSymbol = str.replaceAll("°", "");

Where str contains you temperature data.

Jabir
  • 2,776
  • 1
  • 22
  • 31
1

try this

    String s = "0°C (32°F)".replaceAll("[\u0080-\u00FF]", "");

or if you have HTML character references in your text use

String s = "Min temp: 0°C (32°F)".replaceAll("&#x.+?;", "");
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Thank you for your help. It works, but i can use this line for one case when the weather is 0C and 32F but my api get the weather info itself so I need something more general to change all degree signs. Somethig like whenever there is `°` replace it with °. – Alex Jj Mar 31 '13 at 12:54
1

If using ASCII character encoding in your codes, when you saved your code, did your IDE asked you in what format you want to save it. Because in Eclipse IDE, if you are using an ASCII character, it prompts you to save your code in UTF-8 format. Hope this helps.

SHA33
  • 115
  • 9
  • Yes that came up and actually I dont rememebr which one I chose, first time I chose 'ok' and nothing happened but next time I chose UTF-8 and my code did not work so i re-do what i did. – Alex Jj Mar 31 '13 at 12:44
1

You need to know that :

° :

  • is Unicode Character for 'DEGREE SIGN'
  • Encoding :HTML Entity (hex)

so if this is the just problem you have (i mean this is the only character you use "Degree sign") , so you can convert it manually Like that :

String s = "Min temp: 0°C (32°F)".replaceAll("°", "°");
System.out.println(s);

if this is not the special character you have , so you may use : Class StringEscapeUtils or jsoup library to convert it to java

Alya'a Gamal
  • 5,624
  • 19
  • 34
  • Thank you it worked well, but the outcome is 0 and 32 for all cases, actually it should get the weather from the api and each time is different. I wish there was a way to change only temperate sign and now the numbers. – Alex Jj Mar 31 '13 at 12:42
  • As I said I only write `String weather = "Min temp: " + day.getInfo("Min Temp");` for any chosen location and the api give me the info of that location, which in that info the degree sign in like code. So the solution for changing the degree sign should be general to work for all location. – Alex Jj Mar 31 '13 at 13:45
  • 1
    are this sign hex code is changed from location to another?? , it will be constant , and if you really have a problem with this case so try to use one of the libraries – Alya'a Gamal Mar 31 '13 at 13:48
  • Thanks, can you tell me how to use libraries? I mean how to add them ? – Alex Jj Mar 31 '13 at 14:51
  • 1
    if you want to add them : please check this answer :http://stackoverflow.com/a/325545/1743852 – Alya'a Gamal Mar 31 '13 at 15:00