wood burning stoves
The moose likes Java in General and the fly likes How to trim double byte spaces? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to trim double byte spaces?" Watch "How to trim double byte spaces?" New topic

How to trim double byte spaces?

yo nara
Ranch Hand

Joined: Apr 03, 2002
Posts: 35
I have string output in japanese.
is there any method in java api to trim the string content?
One method I know is that convert double byte spaces
to single byte spaces and them use trim.
but still is there any good method to do it?
thnx in advance.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Have to admit I haven't worked with double-byte-code strings, so this may be off base ... I have my own trim method (actually called strip() after the REXX language) that lets you specify whether to trim leading, trailing or both and what character you want to remove. So to remove leading zeros from a string:

Would something like that work for you? It's a nice generalized routine to keep around.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
It's hard to say here without seeing more of the code you use to read or write the values you're talking about. However I suspect that at one or more points in your system you're using the wrong encoding. In general, anytime you're converting between raw bytes and chars, you should probably be using one of these:
new InputStreamReader(InputStream in, String encoding)
new OutputStreamWriter(OutputStream out, String encoding)
new String(byte[] array, String encoding)
String.getBytes(String encoding)
(There are also various things in java.nio.charset.* which operate similarly.)
All these use the name of a charset as the encoding parameter. If you're not specifying the encoding, you're probably getting a default which is not what you want. If you don't know what encoding you're using, you need to find out. Are you getting input from a text file, or a database, or a socket steam, or what? Similarly, what are you writing to? Typically both input and output need to have a particular encoding (not necessarily the same), andyou need to find out what the expected encoding is. Or you can jsut experiment with changing the encoding value around; maybe you'll get lucky. Common choices for Japanese would probably be "shift-jis", "utf-16", or "utf-8". (There are a number of subtle variants on these, but chances are good that one of these three will be close enough to fix 99% of your problems, which is a good place to start at least.) Beware that there are generally a number of places throughout a system where there's translation between bytes and chars, and you may have to look at all of them. If encoding is screwed on during input, you can't usually fix it during output. Also it's possible the problem isn't with your Java program at all, but with whatever program you're using to look at the output with.
Sorry if this isn't more helpful. Try answering these questions:
Where did the data come from? What format was it in?
How exactly did it get converted to a String or char[] array in your Java program?
How does the data get output from your Java program?
How are you viewing the output?
Your problem could be anywhere within one of these step. Good luck.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://aspose.com/file-tools
subject: How to trim double byte spaces?
It's not a secret anymore!