I am writing this function for a j2me application, so I have some advanced / modern Java classes available for me . I'm getting on java.lang.ArrayIndexOfOfBoundsException. Therefore, apparently either I do not like it like the new Chars array is started, or I am not doing anything right while calling the system. ArrayPP () Any and all advice is appreciated!
/ * * Any leading and trailing spaces * / public static string trim {string [st] {char] chars = str.toCharArray (); Int len = chars.length; // Main timing (lane and gt; 0) & amp; amp; amp;; (character [0] == '')) {char [] newChars = new char [] {}; // Start the empty array system. Arrayopy (Characters, 1, New Chairs, 0, Len-1); Characters = new Chars; Len = chars.length; } // TODO: Returning characters. String (); }
The easiest way to trim the forward and trailing white space is to call. If you just want to trim the major and the following spaces (instead of all major and subsequent whitespaces), then there is an Apache Commons method that can do this; Call it as 2 code as ""
.
There are several bugs in your attempt code, and are fundamentally disabled.
- Count of key place characters
- Count back space characters
- call
string. Substring (to, end)
To create a new string, the letters you want to keep.
This approach avoids copying any characters 1 .
1 - Actually, this depends on the implementation of the string
. There will be no copy for some implementation, a copy has been made for others. But there is an improvement on your view, in which there are at least 2 copies, and if there is any letter to trim.
Comments
Post a Comment