I Spy Code - Java

Remove the last character from a string

Question:

Write a java program that removes the last character from a string.

Solution:

Here is a java example that shows how to remove the last character from a string:

Source: (Example.java)

import org.apache.commons.lang.StringUtils;
 
public class Example {
 
   public static void main(String[] args) {
 
      String str1 = "the cat in the hat.";
      System.out.println(str1);
 
      // remove the period
      String str2 = StringUtils.chop(str1);
      System.out.println(str2);
 
   }
}
 

Output:

$ java Example
the cat in the hat.
the cat in the hat

References:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#chop-java.lang.String-

Questions answered by this page:

How to remove the last character from a string with java?
Java example that deletes the last character from a string