|
-
December 23rd, 2002, 04:28 PM
#1
working with strings
Hi
I am fairly new to java, I 've got some background in visual basic
there is two function that I used a lot in VB
Left(String, Integer) to remove a number of characters from the left of a string
Ex: Left("Hello", 2)will give "He"
Right(String, Integer) to remove a number of characters from the right of a string
Ex : Right("Hello", 2) will give "lo"
I came up with this two methods in Java to deal with this
Code:
public String left(String s, int len) {
StringBuffer str= new StringBuffer("");
for (int j = 0;j < len;j++) {
str.append(s.charAt(j));
}
return str.toString();
}
public String right(String s, int len) {
StringBuffer str = new StringBuffer("");
for (int j = s.length()-len;j < s.length();j++) {
str.append(s.charAt(j));
}
return str.toString();
}
Question :
Is There a proper method In java tha really deals with this issue or shall I stick to my methods thank you
-
December 23rd, 2002, 04:49 PM
#2
When you want to find out what you can do with a class, look at the JavaDocs first. It will save you wasting time reinventing the wheel. If you look at the String class, you'll find plenty of useful methods, including two flavours of substring(...).
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|