• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to trim double byte spaces?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The knights of nee want a shrubbery. And a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic