1

I'm trying to transfer the last character of a StringBuffer to the beginning by using StringBuffer.insert(). I want to be able to do this even if I don't know the word in advance.

I tried to transfer the letter using the insert() and length() methods below, but it did not work.

    StringBuffer str3 = new StringBuffer("Colour");
    System.out.println(str3.insert(0,str3.length()));

    // output is "6Colour"
AbsoluteSpace
  • 710
  • 2
  • 11
  • 21
Daniil Nedostup
  • 57
  • 1
  • 10

5 Answers5

0

When you insert str3.Length() at position 0 you're inserting the length, as an int, at that position instead of the last character of str3.

Try the below code to insert the character instead:

StringBuffer str3 = new StringBuffer("Colour");

char lastChar = str3.charAt(str3.length()- 1);

System.out.println(str3.insert(0, lastChar));

You might find this article helpful to learn more about StringBuffer.Insert().

Edit:

As @Nexevis mentioned if you want to remove the StringBuffer's last character this answer says you can do this: str3.setLength(Math.max(str3.length() - 1, 0)); to remove it.

This Math.max() ensures that .setLength() won't set the length to be a negative number if the string size is 0.

AbsoluteSpace
  • 710
  • 2
  • 11
  • 21
  • He says "transfer", in your scenario you copy it and put it in the front resulting it in the front and end, maybe he wants to remove the last character? It isn't the clearest though. For example "Colour" would be "rColou" not "rColour" – Nexevis Jul 11 '19 at 19:47
0

To bring the last letter to the beginning, we can use the CharAt to find the last letter and do insert.

    StringBuffer str3 = new StringBuffer("Colour");

    System.out.println(str3.insert(0,str3.charAt(str3.length()-1)));

=>'rColour

venkat
  • 1
  • 1
0

The problem with your code is that you are using the length of string.

Try the code below:

String str = "Colour"; // Some user String

StringBuffer str3 = new StringBuffer(str);

char lastChar = str3.charAt(str3.length()- 1); // Get The last Character

str3 = str3.insert(0, lastChar); // Insert last char to firs index

str3 = str3.deleteCharAt(str3.length - 1); // Delete the last char

System.out.println(str3); // Print the string
Mohammed Junaid
  • 1,362
  • 14
  • 18
0

If you want to remove the last character and put it at the beginning of the string. 'Colour' --> 'rColou'

Generating a new String, only for display for example:

StringBuffer str3 = new StringBuffer("Colour");
String s = str3.charAt(str3.length()-1)  + str3.substring(0, str3`enter code here`.length()-1);

Keeping the same variable:

StringBuffer str3 = new StringBuffer("Colour");
str3.insert(0, str3.charAt(str3.length()-1));
str3.deleteCharAt(str3.length()-1);

Ps.: don´t forget to test for NULL first on str3

Ddasol
  • 35
  • 7
0
private static String transferLastCharToFirstPosition(String input) {
    if (input == null || input.isEmpty() || input.length() == 1) return null;
    char lastChar = input.charAt(input.length() - 1); //get last char
    input = input.substring(0, input.length() - 1); //remove the last char
    input = String.valueOf(lastChar).concat(input); // put the last char on first place
    return input;
}
f.trajkovski
  • 794
  • 9
  • 24