Deleting last char in a string...

I have a string that is created as follows: strBuffer += k.getKeyChar();(k is KeyPressed ...KeyEvent) How can I delete the last Char in the string strBuffer?

Hi
// simply substring
if (strBuffer.length() > 1)
    strBuffer = strBuffer.substring(0, strBuffer.length()-1);
// you may also use
if (strBuffer.length() > 1)
    StringBuffer buffer = new StringBuffer(strBuffer).
                               deleteCharAt(strBuffer.length()-1);
    strBuffer = buffer.toString();
}I've not tested the above code myself & I've written the above code on the go after reading your query. The above code definitely work & help you.
Regards,
JPassion

Similar Messages

  • How to delete the last char in a String?

    i want to delete the last char in a String, but i don't want to convert the String to a StringBuffer or an array, who knows how to do?

    Try it in this way
    String MyString = "ABCDEF";
    MyString = MyString.substring(0,MyString.length()-1);

  • Deleting the last char of a string

    How can I Delete the last char of a string???
    example:
    asd@fdg@vvdfgfd@gdgdfgfd@gdfgdf@
    I want delete the last @!
    Tks in advance!

    hi,
    try this:
    lv_count = strlen(string).
    lv_count_last = lv-count - 1.
    replace string+lv_count_last(lv_count) in string by ' '.
    This should work.
    thnx,
    ags.
    Edited by: Agasti Kale on Jun 4, 2008 9:03 PM

  • How to remove last char in a string by space

    I have to implement backspace application(remove a last char in a string , when we pressed a button).
    for ex: I enter the no
    1234
    instead of 1236
    So when i press a button on the JWindow... it should display
    123
    so that i can enter 6 now.
    I tried to display the string "123 " instead over "1234" but it is not working when the no background is specified. but works fine when a background color is specified.
    The string is displayed as
    Graphics2D g = (Graphics2D)window.getGraphics();
    AttributedString as = new AttributedString(string, map);
    g.drawString(as.getIterator(),x,y);
    In the map, the background is set to NO_BACKGROUND.
    Thanks and regards

    Deja vu. I saw this kind of post before, and I'm sure
    it was today.http://forum.java.sun.com/thread.jspa?threadID=588110&tstart=0
    Here it is.

  • Finding last char in the string

    Hi
    i need to find out whether the last char of a string is '/' (forward slash). please let me know if there's a FM to do that. Other post me the logic for this ASAP.
    thanks

    len = strlen(char).
    len = len - 1.
    if char+len(1) = '/'.
      *write ur code
    endif.
    chk this code
    REPORT ychatest.
    DATA : str(5) VALUE '123/',
           len TYPE i.
    len = STRLEN( str ).
    len = len - 1.
    IF str+len(1) = '/'.
      WRITE : '/ found'.
    ENDIF.
    <b>Reward points if helpful and close thrad if solved</b>
    Message was edited by: Chandrasekhar Jagarlamudi

  • How to delete last character in a string

    Guys i have a question...
    how can i delete the last character in a string..
    for example
    String myString = "helloWorld, ";
    how do i delete the last character in this string which is a "," ?

    String newString = myString.substring(0, myString.length()-1);

  • Delete last char from a console window.

    Hey all, could be a silly question.......
    Is it possible to delete a char from a console window?
    e.g.
                     System.out.print("-");
                   System.out.print("|");
                   System.out.print("/");
                   System.out.print("\\");I'm trying to give the illusion of the line spinning, but cant delete the last printed character. I've tried adding "\b" but with no joy.
    And help would be nice, as it's the little things that always take the longest!
    T.

    @Op. You can use JCurses if you accept that your code might not execute on all platforms.
    http://sourceforge.net/projects/javacurses/

  • Read last char of a String

    I have the following code that functions like a basic RPN calculator:
    public class Main {
         * @param args the command line arguments
         static public ArrayList<Double> stack = new ArrayList<Double>();
         static double numberInput;
         static double answer;
         static String operation;
    //Prints Welcome screen, initialized checkforInput
         public static void main(String[] args) {
             System.out.println();
             System.out.println("Welcome to RPNCalc by theCoffeeShop()");
             checkForInput();
    //Checks for input, redirects to appropriate method
         public static void checkForInput() {
             Scanner reader = new Scanner(System.in);
          String input = reader.nextLine();
             if(isDouble(input) == true) {
                double number = Double.parseDouble( input );
                stack.add(0, number);
                repeat();
             } else {
                operation = input;
                operation();
    //Processes numeric operations
         public static void operation () {
             if(operation.equals("+")) {
                 answer = stack.get(1)+stack.get(0);
                 stack.add(0, answer);
                 System.out.println(answer);
                 repeat();
             } else if (operation.equals("-")) {
                 answer = stack.get(1)-stack.get(0);
                 stack.add(0, answer);
                 System.out.println(answer);
                 repeat();
             } else if (operation.equals("*")) {
                 answer = stack.get(1)*stack.get(0);
                 stack.add(0, answer);
                 System.out.println(answer);
                 repeat();
             } else if (operation.equals("/")) {
                 answer = stack.get(1)/stack.get(0);
                 stack.add(0, answer);
                 System.out.println(answer);
                 repeat();
             } else {
                 System.out.println("Incompatible operation");
                 repeat();
    //Called when ready to recheck for input
         public static void repeat() {
             checkForInput();
    //Checks if string is double
         public static boolean isDouble( String input )
            try
                Double.parseDouble( input );
                return true;
            catch(Exception e)
               return false;
    }To perform a basic operation (5+2), you would do the following
    5
    enter
    2
    enter
    (plus)
    enter
    Computer prints 7.
    I would like it to function like this:
    5
    enter
    2(plus)
    enter
    Computer prints 7.
    To do this, I have to be a be to split a string into 2 parts, the operation and the number. I think I could do this with a StringBuffer, but How to read the LAST char of a StringBuffer, instead of just a specific char? If there is a way to do it w/out a StringBuffer, I'm open to suggestions.

    Hint:
    The last character of a String (or StringBuilder or StringBuffer, etc.) is at the index (length_of_string - 1).
    Are you sure you're supposed to solve the problem this way? Usually homework assignments of this sort involve writing a whole parser.

  • Deleting last character of a string

    Hi Abapers,
    I want to delete the last character of my workarea and then store into table.
    The workarea says that it is FAX|
    I have to remove the last character.
    How to do tht.
    Please reply.
    Thanks

    hi
    do this in following way
    frist find the String length
    let it will be
    w_str = 'sdn is wonderfull'
    now use.
    w_len = strlen(w_str) .
    now
    w_result = w_str+0(w_len - 1).
    Cheers
    Snehi

  • How do you restrict new line in input field or delete last char. in as3?

    Sorry, Noobie here!
    I have a text input box. If the enter key is pressed, a new line begins; thus making the previous line invisible unless the backspace key is pressed.
    So, if i type "1" into the input box, then hit eneter the number "1" becomes invisible, unless I hit back.
    Now, I still need the enter key because i have an event listener that calls a function once it is pressed, but i still need that "1" to be visible.
    So, I need one of either:
    To be able to restrict the input text field from creating a new line (making max line allowed= 1)
    or
    To delete the last character entered in the input text if the enter key is pressed.
    Both of these should have the same result, however I don't know how to do both.
    If anyone can help, i'd be very grateful!

    This worked! Thanks so much!

  • Remove last char of string

    Hi Guys
    How can I remove the last char of a String ?

    With [the substring method|http://java.sun.com/javase/6/docs/api/java/lang/String.html#substring(int,%20int)] .

  • Remove last char

    Ok i am trying to make just a simple calculator program for a class. I am trying to get the backspace button to work. How would i delete the last char in the string.

    use a substring, the last char. will be:
    myString.length
    that will give you the length and from that you know what the last char. is

  • The last character of a string

    How to judge the last char of a string is " ?
    if (s.endsWith("""))or
    if (s.endsWith("/""))

    bet
    you just copied and pasted what the OP had written
    and quickly put that
    backslash in, hit post without previewing first just
    to make me look like
    a slow old sod! It's not fair ;-)
    kind regards,
    JosNope, I used the Alt-[numeric code] combinations to manually type each character. Cut-n-paste is for wimps, and actually typing single-stroke keys is for milquetoasts.
    Alt-[numeric code] is for real men ;-)

  • JTextField: delete last entered char

    how can i delete the last entered char in a JTextField.
    In my textfield a Person can only enter 5 Chars. If you enter mor than 5 chars, the chars behind the 5th position must be deletet an the caret must stay behind the 5th char.
    How can i do this in java??
    Java let always stay the last typed char behind my string, i cant remove it.
    2.Question: with the method .getText() i get a String, but the last pressed key is not in that string
    how can i make it right??

    First Construct this method
         public static void setLenText(java.awt.event.KeyEvent event,int len){
         if (event.isConsumed())
    return;
    char c = event.getKeyChar();
    JTextComponent source = (JTextComponent )event.getSource();
    if (source.getText().length() >= len &&
    event.getKeyChar() != event.VK_BACK_SPACE &&
    source.getSelectionStart() == source.getSelectionEnd())
    source.getToolkit().beep();
    event.consume();
    then in the KeyTyped event of the textfield:
    void JTextField_keyTyped(java.awt.event.KeyEvent event)
              GlobalMgr.setLenText(event,5);
    By

  • Delete a char from string ?

    Hi,
    I want to delete a char from string. i used the following function.
    String f = formulla.replace('[','');
    The above function doesnt work as it tells me to put a space or some char in 2nd parameter which i dont want. i just want to delete all occurences of some specific char in a string.
    Any suggestion.
    Thanks alot.

    u can do:
    String before;
    char charToReplace;
    StringBuffer tempBuf = new StringBuffer(before);
    for (int i=0; i<tempBuf.length(); i++)
            if (tempBuf.charAt(i)==charToReplace)
                  tempBuf.deleteCharAt(i);
    String after = tempBuf .toString(); HTH
    Yonatan

Maybe you are looking for

  • Can a PO be reopened after its closed automatically.

    We are in implementation phase of SAP B1. We are using non perpetual inventory system. We created a Purchase order to buy 100 KG of item. Then we created 3 MR of 20 KG receipt each. We realized we have received one of MR in wrong warehouse, so we nee

  • Photos did not download and I deleted them from memory card. how to recover?

    Photos did not download to computer.  I deleted them from memory card.  Is there a way to retrieve them from memory card.

  • Is there a way to actually sync music from my iPhone 3G to my computer?

    Recently, I had to get a new laptop. The music on my iPhone won't trasfer to my new laptop.  I have put new, different music on my laptop.  When I try to sync the two, laptop & iPhone, all it wants to do is to wipe out the music on my iPhone and repl

  • Topic Over OC4J with MessageDriven Bean

    Hi, I have two oc4j instances 10.1.3.1 running on same box. In instance1, i have Topic and TopicConnectionFactory configured. And on the same instance1 i have a sample application publishing TextMessage into Topic. On the instance2, i have configured

  • Java Applets with AWT

    Hi, I have the problem to display the applet with AWT. If I am running the same applet in the internal server it works fine.If I am running through the web its giving Class not found exception.That web server also is mapped into the same server. Here