Trim leading white space with PHP?

Strip all whitespace from the left end of the title: <?php echo ltrim(wp_title(”)); ?> Strip all whitespace from either end: <?php echo trim(wp_title(”)); ?> Strip all spaces from the left end of the title: <?php echo ltrim(wp_title(”), ‘ ‘); ?> Remove the first space, even if it’s not the first character: <?php echo str_replace(‘ ‘, … Read more

String strip() for JavaScript? [duplicate]

Use this: if(typeof(String.prototype.trim) === “undefined”) { String.prototype.trim = function() { return String(this).replace(/^\s+|\s+$/g, ”); }; } The trim function will now be available as a first-class function on your strings. For example: ” dog”.trim() === “dog” //true EDIT: Took J-P’s suggestion to combine the regex patterns into one. Also added the global modifier per Christoph’s suggestion. … Read more

How can I configure Entity Framework to automatically trim values retrieved for specific columns mapped to char(N) fields?

Rowan Miller (program manager for Entity Framework at Microsoft) recently posted a good solution to this which uses Interceptors. Admittedly this is only valid in EF 6.1+. His post is about trailing strings in joins, but basically, the solution as applied neatly removes trailing strings from all of the string properties in your models, automatically, … Read more

What is a good alternative of LTRIM and RTRIM in Java?

With a regex you could write: String s = … String ltrim = s.replaceAll(“^\\s+”,””); String rtrim = s.replaceAll(“\\s+$”,””); If you have to do it often, you can create and compile a pattern for better performance: private final static Pattern LTRIM = Pattern.compile(“^\\s+”); public static String ltrim(String s) { return LTRIM.matcher(s).replaceAll(“”); } From a performance perspective, … Read more

Difference between String trim() and strip() methods in Java 11

In short: strip() is “Unicode-aware” evolution of trim(). Meaning trim() removes only characters <= U+0020 (space); strip() removes all Unicode whitespace characters (but not all control characters, such as \0) CSR : JDK-8200378 Problem String::trim has existed from early days of Java when Unicode had not fully evolved to the standard we widely use today. … Read more