CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2002
    Location
    France
    Posts
    70

    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

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    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 &#91;CODE]...your code here...&#91;/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
  •  





Click Here to Expand Forum to Full Width

Featured