trim() won't work for MSIE4.x

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
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

fontaineConnect With a Mentor Commented:
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]) + "*");


    // 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] <= ' ')) {

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

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

    public static void main(String args[]) {
        Test test = new Test();
mitrakisAuthor Commented:

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

Best regards

mitrakisAuthor Commented:
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
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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?
mitrakisAuthor Commented:

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

The chars B and V seem to be from "" 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
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:

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

sPadding += new String(b, 0, 0, nread); // JDK 1.0.2 way...
mitrakisAuthor Commented:
pretty kewl !

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

Best regards and THX again

increased pts. to 75...hope this is ok.
All Courses

From novice to tech pro — start learning today.