Solved

trim() won't work for MSIE4.x

Posted on 1998-11-20
7
214 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 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