wood burning stoves*
The moose likes Beginning Java and the fly likes replacing characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "replacing characters" Watch "replacing characters" New topic
Author

replacing characters

Fernando Dominguez
Ranch Hand

Joined: Sep 01, 2006
Posts: 102

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

Joined: Nov 18, 2008
Posts: 19
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

Joined: Sep 01, 2006
Posts: 102

Thanks for your interest.

replaceAll works well with (') quote,

but does not work with (�) spanish accent.

Thanks
vipin jos
Greenhorn

Joined: Nov 18, 2008
Posts: 24
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

Joined: Sep 01, 2006
Posts: 102

It worked.

Many Thanks.
Yong Mook Kim
Greenhorn

Joined: Nov 17, 2008
Posts: 6
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

Joined: Sep 01, 2006
Posts: 102

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

Joined: Oct 13, 2005
Posts: 38463
    
  23
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

Joined: Nov 17, 2008
Posts: 6
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

Joined: Oct 13, 2005
Posts: 38463
    
  23
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

Joined: Nov 18, 2008
Posts: 24
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

Joined: Sep 01, 2006
Posts: 102

Sorry for being so late.

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

Bye
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

b4? Are we playing Bingo?

Please Use Real Words.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
vipin jos
Greenhorn

Joined: Nov 18, 2008
Posts: 24
Hi Rob,

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

Thanks,
Vipin
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Ah, you're right. I missed your post when I read his

My apologies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: replacing characters