Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Trim String

  • 17-04-2009 02:10PM
    #1
    Registered Users, Registered Users 2 Posts: 2,607 ✭✭✭


    Please help this is annoying me. Ive a string which I got from a combobox (.getSelectedItem()) ive assigned it to a string variable, However I only want the first letter of the string.

    Can I use some sort of Left() function to do this?


Comments

  • Closed Accounts Posts: 6,151 ✭✭✭Thomas_S_Hunterson




  • Registered Users, Registered Users 2 Posts: 1,181 ✭✭✭ronkmonster


    you can use .substring(int start,int end):

    so .substring(0,1);


  • Registered Users, Registered Users 2 Posts: 56 ✭✭jarmstrong001


    See String API at http://java.sun.com/j2se/1.5.0/docs/api/

    use charAt(0) to get the leftmost character in the String

    charAt

    public char charAt(int index)

    Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

    If the char value specified by the index is a surrogate, the surrogate value is returned.

    Specified by:
    charAt in interface CharSequence

    Parameters:
    index - the index of the char value.
    Returns:
    the char value at the specified index of this string. The first char value is at index 0.
    Throws:
    IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.


  • Registered Users, Registered Users 2 Posts: 2,607 ✭✭✭VinylJunkie


    Yeah ive tried subString with no success! Ill try it again and see what happens, thanks for the quick reply's.

    edit: It works I must have done it wrong first time around

    Thanks Again


  • Closed Accounts Posts: 6,151 ✭✭✭Thomas_S_Hunterson


    See String API at http://java.sun.com/j2se/1.5.0/docs/api/

    use charAt(0) to get the leftmost character in the String

    charAt

    public char charAt(int index)

    Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

    If the char value specified by the index is a surrogate, the surrogate value is returned.

    Specified by:
    charAt in interface CharSequence

    Parameters:
    index - the index of the char value.
    Returns:
    the char value at the specified index of this string. The first char value is at index 0.
    Throws:
    IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.

    The only potential problem with this is that it returns a char as opposed to a String, but it could be the one for the job, depending on what the op is looking for.


  • Advertisement
  • Closed Accounts Posts: 6,151 ✭✭✭Thomas_S_Hunterson


    Yeah ive tried subString with no success! Ill try it again and see what happens, thanks for the quick reply's.

    edit: It works I must have done it wrong first time around

    Thanks Again
    Java is case sensitive! The method is called substring not subString!


Advertisement