Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Trim String

  • 17-04-2009 2: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