Solved

trim() won't work for MSIE4.x

Posted on 1998-11-20
7
188 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

705 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

19 Experts available now in Live!

Get 1:1 Help Now