Truncate String in Java Example

(Last Updated On: August 11, 2016)

Removing First Few Characters Java Program:

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());
	}

}

Output:

String:  www.Javadomain.in
Truncating First 4 characters:  Javadomain.in

 

 

Removing Last Few Characters Java Program:

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);
}

}

Output:

String:  www.Javadomain.in
Truncating Last 3 characters:  www.Javadomain

224 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *