Solved

trim() won't work for MSIE4.x

Posted on 1998-11-20
7
191 Views
Last Modified: 2010-03-30
In my applet I use trim() to remove whitespaces from a string.
Unfortunately, I noticed that browsers with e.g. Java1.1.2 return an error on this method.
Browsers with e.g. Java1.1.5 work fine (MSIE4.x not at all).
Are there any (maybe simple) altenatives I dunno ?
I just want to remove leading spaces in my string.

Best regards
-Stavi-
0
Comment
Question by:mitrakis
  • 4
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 70 total points
ID: 1227939
If the implementation of trim() causes you problems with some VMs, you will have to
provide your own implementation. The ideal would be to subclass String and
override the trim() method by an home made one. However, String is a final class
and can not be subclassed (final classes are more easy to optimize, this is why the
String class is final). You will have to implement a trim() method as part of a utility
class, or directly as a private utility method part of the class that needs access to
the functionality, like in the following example:

public class Test {

    public Test() {
    }

    public void doIt() {
        String strings[] = {"aaa", "   bbb   ", "ccc", "  ddd   eee   "};

        for(int i=0; i< strings.length; i++) {
            System.out.println("*" + trim(strings[i]) + "*");
        }

        return;
    }

    // utility method to trim a String.
    // The following code is deeply inspired from the source code of SUN's JDK
    // String.trim() method. It is hence functionaly equivalent.

    private String trim(String toTrim) {
        int count = toTrim.length();
        int len = count;
        int st = 0;
        int off = 0;      
        char[] val = new char[len];

        toTrim.getChars(0, len, val, 0);    

        while ((st < len) && (val[off + st] <= ' ')) {
            st++;
        }

        while ((st < len) && (val[off + len - 1] <= ' ')) {
            len--;
        }

        return ((st > 0) || (len < count)) ? toTrim.substring(st, len) : toTrim;
    }

    public static void main(String args[]) {
        Test test = new Test();
        test.doIt();
        return;
    }
}
0
 
LVL 3

Author Comment

by:mitrakis
ID: 1227940
fontaine,

let me test this on monday, then I'll give feedback.
THX for help.

Best regards
-Stavi-


0
 
LVL 3

Author Comment

by:mitrakis
ID: 1227941
Oh no...something happened I didn't expect...=:-((

Not trim() is the problem, it's String (byte[]) instead !
Now, where trim() is "replaced", I get an error again, so I found out that it's String(byte[]) which causes all my problems...

I think I have to use something like String(StringBuffer) to make my applet available for almost all browsers ?!

If you could support me on how to read byte by byte with "StringBuffer" I'll increase points to 75.
Please tell me if this is not ok for you...(then I'll try to post a new Q !) in any case you'll get current credits coz your suggestion worked for me.

Best regards
-Stavi-
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:fontaine
ID: 1227942
Could you verify that you are actually not trying to pass a null array? Do something like:

byte bytes[] = ...

if (bytes == null) {
      System.out.println("bytes == null");
} else {
      System.out.println("bytes <> null");
}

String myString = new String(bytes); etc.

and have a look at the Java console.

Also, what is exactly the error you have, a NullPointerException?
0
 
LVL 3

Author Comment

by:mitrakis
ID: 1227943
fontaine,

definitely not NULL.

here's the error msg from the console:
# Applet exception: java.lang.String: method <init> ([BII)V not found
java.lang.NoSuchMethodError: java.lang.String: method <init> ([BII)V not found at StreamLstnr.run(StreamLstnr.java:42)

The chars B and V seem to be from "BVLAB12.fh-reutlingen.de" which is the name of the machine I connect to and which I have to analyze in my applet.

line 42 in code is:
sPadding += new String(b, 0, nread);

where b is:
byte b[] = new byte[cc.MAXBUFLEN];
and nread from type "int"

THX a lot for help
-Stavi-
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1227944
OK, it seems that the browser does not support JDK 1.1 (new String(byte[], int , int) is a JDK
1.1 constructor). Try out the following:

Before:
sPadding += new String(b, 0, nread);

After:
sPadding += new String(b, 0, 0, nread); // JDK 1.0.2 way...
0
 
LVL 3

Author Comment

by:mitrakis
ID: 1227945
pretty kewl !

never thought that this could work...but it works =:-))

Best regards and THX again
-Stavi-

P.S.:
increased pts. to 75...hope this is ok.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML Paring  Error - Premature end of file. 7 77
topping2 challenge 13 80
servlet doXXX methods 3 33
varialbe initialization 11 30
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now