struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matsuha...@quick.co.jp
Subject Re: [HELP] I couldn't trim() 1 japanese String (charset UTF-8)
Date Tue, 21 Jun 2005 12:02:20 GMT

Do you want to remove leading/trailing double-bytes-whitespace chars?

I mean the double-bytes-whitespace to be a UNICODE char of \u3000.

The java.lang.String#trim() does not see the \u3000 as a whitespace which
it should chop off.

To remove the \u3000 char, I think you would need fair amount of custom
code. Following dirty code may suggest you something.


---------------------------------------------------------------
class DoubleBytesWhitespaceTrimmer {

    static char doubleBytesWhitespace = '\u3000' ;

    public static String trim(String s) {
      String leadingTrimmed = trimLeadingChar(s , doubleBytesWhitespace);
      String rev = reverse(leadingTrimmed);
        return reverse(trimLeadingChar(rev , doubleBytesWhitespace));
    }

    private static String trimLeadingChar(String s , char c) {
      char[] chars = s.toCharArray();
        StringBuffer b = new StringBuffer();
        int index;
        for (index = 0; index < chars.length; index++) {
            if (chars[index] == c)
                  ;
            else
                  break;
        }
        for ( ;index < chars.length; index++) {
            b.append(chars[index]);
        }
        return b.toString();
    }

    private static String reverse(String s) {
      char[] chars = s.toCharArray();
      StringBuffer b = new StringBuffer();
      for (int x = chars.length - 1; x >= 0 ; x--) {
            b.append(chars[x]);
      }
      return b.toString();
    }

    public static void main(String[] args) {
        StringBuffer b = new StringBuffer();
        b.append(doubleBytesWhitespace);
        b.append("foo");
        b.append(doubleBytesWhitespace);
        System.out.println("source : [" + b.toString() + "]");
        String trimmed= DoubleBytesWhitespaceTrimmer.trim(b.toString());
        System.out.println("trimed : [" + trimmed + "]");
        System.out.println("\"foo\".equals(\"" + trimmed + "\") ? " +
"foo".equals(trimmed));
    }
}
-----------------------------------------------


This gives following output for me.
-----------------------------------------------
source : [ foo ]
trimed : [foo]
"foo".equals("foo") ? true






                                                                                         
                                        
                      "Pham Anh Tuan"                                                    
                                        
                      <anhtuan@ichi-co         宛先:    "Struts Users Mailing List" <user@struts.apache.org>
                     
                      rp.jp>                   cc:                                    
                                           
                                               件名:    [HELP] I couldn't trim() 1 japanese
String (charset UTF-8)                
                      2005/06/21 20:03                                                   
                                        
                      "Struts Users                                                      
                                        
                      Mailing List" へ                                                  
                                         
                      返信してください                                           
                                                
                                                                                         
                                        
                                                                                         
                                        




Hi all,

I got 1 problem when I trying to eliminate spaces at ride side and left
side of 1 japanese String.

In mySQL I set UTF-8 for both database and my Jsp pages. But in Action, I
can't not trim() japanese string, so, when I insert that string to
database, spaces are exist.

Plz help me solve this problem! :(

Thanks for ur reading.

Pham





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message