Solved

trim() won't work for MSIE4.x

Posted on 1998-11-20
7
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

632 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