Newbie string processing query
From: Tim Wright (tim.wright_at_nospam.informa.com.invalid)
Date: 02/18/04
- Next message: Steven Wurster: "Re: Template method pattern in Java ???"
- Previous message: Andrew Thompson: "Re: Cannot access class"
- Next in thread: Andrew Thompson: "Re: Newbie string processing query"
- Reply: Andrew Thompson: "Re: Newbie string processing query"
- Reply: Kai Grossjohann: "Re: Newbie string processing query"
- Reply: Thomas Weidenfeller: "Re: Newbie string processing query"
- Reply: Barry White: "Re: Newbie string processing query"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 18 Feb 2004 13:14:16 +0000
Hi all,
I'm trying to work out an efficient way to convert a String (with
extended characters - ie accents, symbols and so on) into a block of
HTML. So, for example, every instance of unicode 0224 would be replaced
by the "à" entity.
I'm sure there must be an easy way to do this (or at least, a *quick*
way to do it) but at the moment, my solution looks like this:
String unicodeToHTML (String input) {
StringBuffer output = new StringBuffer();
int len = input.length();
String oneChar = "";
for (int i=0; i<len; i++) {
oneChar = input.substring(i,i+1);
if (oneChar.equals("\u0160")) oneChar = " ";
if (oneChar.equals("\u0161")) oneChar = "¡";
// several hundred more "if" statements here...
output.append(oneChar);
}
return output.toString();
}
Whilst this works, it's quite astonishingly slow. I'm sure that when I
create a new String from an array of bytes (with a specified encoding)
that Java must be doing something similar to this (translating said
encoding into unicode) but somehow it does it a couple of hundred times
faster...
Any tips or suggestions would be gratefully received!
Cheers,
Tim.
- Next message: Steven Wurster: "Re: Template method pattern in Java ???"
- Previous message: Andrew Thompson: "Re: Cannot access class"
- Next in thread: Andrew Thompson: "Re: Newbie string processing query"
- Reply: Andrew Thompson: "Re: Newbie string processing query"
- Reply: Kai Grossjohann: "Re: Newbie string processing query"
- Reply: Thomas Weidenfeller: "Re: Newbie string processing query"
- Reply: Barry White: "Re: Newbie string processing query"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|