Truncate String in Java Example
Removing First Few Characters Java Program:
[java]
package in.javadomain;
public class Truncate {
public static void main(String[] args) {
String input ="www.Javadomain.in";
System.out.println("String: "+input);
String truncateFirst = removeFirst(input);
System.out.println("Truncating First 4 characters: "+truncateFirst);
}
private static String removeFirst(String input){
return input.substring(4,input.length());
}
}
[/java]
Output:
[plain]
String: www.Javadomain.in
Truncating First 4 characters: Javadomain.in
[/plain]
Removing Last Few Characters Java Program:
[java]
package in.javadomain;
public class Truncate {
public static void main(String[] args) {
String input ="www.Javadomain.in";
System.out.println("String: "+input);
String truncateLast = removeLast(input);
System.out.println("Truncating Last 3 characters: "+truncateLast);
}
private static String removeLast(String input){
return input.substring(0,input.length()-3);
}
}
[/java]
Output:
[plain]
String: www.Javadomain.in
Truncating Last 3 characters: www.Javadomain
[/plain]