Showing posts with label trim. Show all posts
Showing posts with label trim. Show all posts

Saturday, 12 November 2022

Trim leading character that matches a given predicate

Write a function that takes a string and predicate as input and trim all the characters that matches to given predicate.

 

Signature

public static String trimLeading(final String str, final Predicate<Character> predicate)

 

Find the below working application.

 

TrimLeadCharacterDemo.java

package com.sample.app.strings;

import java.util.function.Predicate;

public class TrimLeadCharacterDemo {

	public static boolean isEmpty(CharSequence str) {
		return str == null || str.length() == 0;
	}

	public static String trimLeading(final String str, final Predicate<Character> predicate) {
		if (isEmpty(str)) {
			return str;
		}

		int counter = 0;
		while (counter < str.length()) {
			if (predicate.test(str.charAt(counter))) {
				counter++;
				continue;
			}
			return str.substring(counter);
		}

		return str.substring(counter);
	}

	public static void main(String[] args) {

		String str1 = trimLeading("abcd", character -> character == 'c');
		String str2 = trimLeading("ccbd", character -> character == 'c');
		String str3 = trimLeading("ccccc", character -> character == 'c');

		System.out.println("str1 : " + str1);
		System.out.println("str2 : " + str2);
		System.out.println("str3 : " + str3);

		str1.trim();

	}

}

 

Output

str1 : abcd
str2 : bd
str3 :

 

Previous                                                 Next                                                 Home

Tuesday, 7 December 2021

Java11: strip vs trim method

 

What is whitespace in Java?

A character is a Java whitespace character if and only if it satisfies one of the following criteria:

 

a.   It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').

b.   It is '\t', U+0009 HORIZONTAL TABULATION.

c.    It is '\n', U+000A LINE FEED.

d.   It is '\u000B', U+000B VERTICAL TABULATION.

e.   It is '\f', U+000C FORM FEED.

f.     It is '\r', U+000D CARRIAGE RETURN.

g.   It is '\u001C', U+001C FILE SEPARATOR.

h.   It is '\u001D', U+001D GROUP SEPARATOR.

i.     It is '\u001E', U+001E RECORD SEPARATOR.

It is '\u001F', U+001F UNIT SEPARATOR

 

How strip is different from trim method?

strip() is "Unicode-aware" evolution of trim().  String::trim has existed from early days of Java when Unicode had not fully evolved to the standard we widely use today.

 

The definition of space used by String::trim is any code point less than or equal to the space code point (\u0020), commonly referred to as ASCII or ISO control characters.

 

StringContainWhiteSpacesDemo1.java 

public class StringContainWhiteSpacesDemo1 {

	private static boolean containOnlyWhiteSpaces(String str) {
		if (str == null) {
			return false;
		}

		return str.strip().isEmpty();
	}

	public static void main(String args[]) {
		String str1 = "Hello";
		String str2 = "\n\t\f\r   \n\t";

		System.out.println("is str1 contains only whitespaces : " + containOnlyWhiteSpaces(str1));
		System.out.println("is str2 contains only whitespaces : " + containOnlyWhiteSpaces(str2));
	}

}

 

Output

is str1 contains only whitespaces : false
is str2 contains only whitespaces : true

 


References

https://bugs.openjdk.java.net/browse/JDK-8200378

 


 

 

 

 

Previous                                                    Next                                                    Home

Wednesday, 24 March 2021

Php: ltrim, rtrim, trim : Trim the string


Function

Description

ltrim

Strip whitespace (or other characters) from the beginning of a string

rtrim

Strip whitespace (or other characters) from the end of a string

chop

Alias of rtrim()

trim

Strip whitespace (or other characters) from the beginning and end of a string

 

Signatures

ltrim ( string $string , string $characters = " \n\r\t\v\0" ) : string

rtrim ( string $string , string $characters = " \n\r\t\v\0" ) : string

trim ( string $string , string $characters = " \n\r\t\v\0" ) : string

 

characters argument specifies the characters that you want to strip, simply list all characters that you want to be stripped.

 

Find the below working application.

 

trim_demo.php

#!/usr/bin/php

<?php

   $str1 = '     Hello      ';

   $ltrim_str = ltrim($str1);
   $rtrim_str = rtrim($str1);
   $trim_str = trim($str1);

   echo "\$str1 : '$str1'\n";
   echo "\$ltrim_str : '$ltrim_str'\n";
   echo "\$rtrim_str : '$rtrim_str'\n";
   echo "\$trim_str : '$trim_str'\n";
   
?>

 

Output

$./trim_demo.php 

$str1 : '     Hello      '
$ltrim_str : 'Hello      '
$rtrim_str : '     Hello'
$trim_str : 'Hello'

 

 

 

 

  

Previous                                                    Next                                                    Home