Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replacing characters

 
Fernando Dominguez
Ranch Hand
Posts: 104
Debian Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to replace a strange character from an string.


replaceAll does not work with � , it returns the same string "asdf�"

I notice that � this character does not exits in the ascii code, � � exits in the extended ascci code but not � alone.

I have looked into the character api but I dont found anything usefull.

Any idea to replace the � from an string?
Is there any method that returns the ascii code from a character?

Thanks.
 
Arun Christopher
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class TestString{
public static void replaceStr(){
String trial1="";
String trial2="arun'";
trial1=trial2.replaceAll("\'","");
System.out.println(trial2);
System.out.println(trial1);

}
public static void main(String args[]){
replaceStr();
}
}
 
Fernando Dominguez
Ranch Hand
Posts: 104
Debian Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your interest.

replaceAll works well with (') quote,

but does not work with (�) spanish accent.

Thanks
 
vipin jos
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check if this helps

String cadena2 = "";
String cadena = "asdf�";
char c = 0xB4;
cadena2 = cadena.replaceAll(""+c,"");
System.out.println("cadena2 : "+cadena2);

I am not sure if this is the character code you have stated.

One URL that can be of help is
http://www.alanwood.net/demos/ansi.html

If checked already, please ignore

Thanks
 
Fernando Dominguez
Ranch Hand
Posts: 104
Debian Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked.

Many Thanks.
 
Yong Mook Kim
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i'm confusing about what you said, because java replaceAll() method working fine for me to replace these characters.



Result

D:\>java TestString
Before : Test 1 = replace'
After : Test 1 = replace
Before : Test 2 = replace┤
After : Test 2 = replace

Any idea why it work for me? I'm using JDK 1.6.0.07
 
Fernando Dominguez
Ranch Hand
Posts: 104
Debian Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you working on a windows machine?

I am working on an UNIX machine
I tried the "original" replaceAll on a windows machine and It worked.

So problem could be the way that both OS/editors uses the caracters.
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fernando Dominguez:
I notice that � this character does not exits in the ascii code, � � exits in the extended ascci code but not � alone.
It is in Unicode; look in Latin-1; its code number is 0x00b4.
 
Yong Mook Kim
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fernando Dominguez:
Are you working on a windows machine?

I am working on an UNIX machine
I tried the "original" replaceAll on a windows machine and It worked.

So problem could be the way that both OS/editors uses the caracters.



Yes, i tested on windows platform. Thanks for info. I will be careful on my UNIX production server when dealing with special characters.
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is peculiar; Java supports the whole of Unicode, and it is usually only a problem on Windows because the Windows command prompt doesn't support special characters. I don't use Unix, but the special character support on Linux which is similar to Unix is usually better than on Windows.
 
vipin jos
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you check what is the value you are getting for the following in Windows and Unix?

System.out.println(""+Integer.toHexString((int)'�'));

In windows I get "b4"
0xb4 is the character code.
 
Fernando Dominguez
Ranch Hand
Posts: 104
Debian Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for being so late.

I get b4 on my eclipse console on a debian linux.

Bye
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b4? Are we playing Bingo?

Please Use Real Words.
 
vipin jos
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

I think Fernando meant character "0xb4" when he mentioned "b4". Not "before".

Thanks,
Vipin
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, you're right. I missed your post when I read his

My apologies.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic