Trim() Vs Strip() in Java 11 Example Program

Trim-vs-script-java-11-example-program-featured
Trim-vs-script-java-11-example-program-blog

Trim():

  • Removes whitespace for unicodes which are less than or equal to \u0020

Strip():

  • Removes whitespace which are even greater than \u0020

Trim() Strip() – Java 11 Sample Program:

package com.ngdeveloper.java11;

public class TrimVsStrip {

	public static void main(String[] args) {
		String s1 = " Mirthbees ";
		System.out.println("trim()  : "+s1.trim());
		System.out.println("strip() : "+s1.strip());

		String s2 = "\t 1site \n";
		System.out.println("trim()  : "+s2.trim());
		System.out.println("strip() : "+s2.strip());

		String s3 = "\\u2000 NgDev \\u2000";
		System.out.println("trim()  : "+s3.trim());
		System.out.println("strip() : "+s3.strip());

		Character c = '\u2000';
		String cs = c + "Saveji" + c;
		System.out.println("trim()  : "+cs.trim());
		System.out.println("strip() : "+cs.strip());		
	}
}

Trim() Strip() – Java 11 Sample Program Output:

trim()  : Mirthbees
strip() : Mirthbees

trim()  : 1site
strip() : 1site

trim()  : \u2000 NgDev \u2000
strip() : \u2000 NgDev \u2000

trim()  : ?Saveji?
strip() : Saveji

What is unicode ?

Numbers, mathematical notation, popular symbols and characters from all languages are assigned a code point, for example, U+0041 is an English letter “A.” Below is an example of how “Computer Hope” would be written in English Unicode. A common type of Unicode is UTF-8, which utilizes 8-bit character encoding.

Leave a Reply