Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem in replacing with the unicode equivalent character ?

807591Feb 18 2005 — edited Apr 11 2008
Hello,

I have a situation wherein i must replace

m with \u3005
n with \u3006
o with \u3041
etc ...,

I get the codepoint value from the multibyte represenation .

But the problem is

I can not replace

String temp="ename";
temp.replace('m','\u3005');

I am not allowed to hard code the value 3005, rather i am suppose to get the code point value from the multibyte representation.
I have no problem in getting the code point value from the multibyte represenation.

After i get the code point value i must concatenate the codepoint value with "\u"

Currently i am following this implementation to do the replacement,


...
String rp=null,snd;
String tmp="ename";
String hh="";
for(int i=0;i<tmp.length();i++)
{
snd=getCodepoint(tmp.charAt(i));
if(snd!=null)
{
rp=replace(String.valueOf(tmp.charAt(i)),
String.valueOf(tmp.charAt(i)),"\\u"+snd);
hh=hh+rp;
}
else
{
hh=hh+String.valueOf(tmp.charAt(i));
}
}
...,
//The replace method

public static String replace(String source, String pattern, String replace)
{
if (source!=null)
{
final int len = pattern.length();
StringBuffer sb = new StringBuffer();
int found = -1;
int start = 0;

while( (found = source.indexOf(pattern, start) ) != -1)
{
sb.append(source.substring(start, found));
sb.append(replace);
start = found + len;
}
sb.append(source.substring(start));
return sb.toString();
}
else return "";

}
...,



Please tell me how to do the replacement
Because when i display after the replacement is done i get

e\u3006a\u3005e for the string "ename"

where as i should be getting special characters in place of 'm' and 'n' in the string

or i must atleast get e?a?e for the string "ename"


Please do offer some suggesstions in this regard
Your suggesstions would be very useful
NOTE:

I am not suppose to make use of the method replaceAll( ) to do the replacements


Thanks and regards

khurram
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 9 2008
Added on Feb 18 2005
8 comments
563 views