Question

I'm looking for a js library like StringUtils of commons-lang in java, which contains a lot of common methods to operating strings.

Such as:

  • IsEmpty/IsBlank - checks if a String contains text
  • Trim/Strip - removes leading and trailing whitespace
  • Equals - compares two strings null-safe
  • startsWith - check if a String starts with a prefix null-safe
  • endsWith - check if a String ends with a suffix null-safe
  • IndexOf/LastIndexOf/Contains - null-safe index-of checks
  • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
  • ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
  • Substring/Left/Right/Mid - null-safe substring extractions
  • SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
  • Split/Join - splits a String into an array of substrings and vice versa
  • Remove/Delete - removes part of a String
  • Replace/Overlay - Searches a String and replaces one String with another
  • Chomp/Chop - removes the last part of a String
  • LeftPad/RightPad/Center/Repeat - pads a String
  • UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
  • CountMatches - counts the number of occurrences of one String in another
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
  • DefaultString - protects against a null input String
  • Reverse/ReverseDelimited - reverses a String
  • Abbreviate - abbreviates a string using ellipsis

It'll be better if it contains some other methods for arrays/date, etc.

Was it helpful?

Solution

String utils - Underscore.string

Object/array utils - Underscore

Date utils - Moment.js

OTHER TIPS

Here we go:

IsEmpty

str.length === 0

IsBlank

str.trim().length === 0

Trim

str.trim()

Equals

str1 === str2

startsWith

str.indexOf( str2 ) === 0

IndexOf

str.indexOf( str2 )

LastIndexOf

str.lastIndexOf( str2 )

Contains

str.indexOf( str2 ) !== -1

Substring

str.substring( start, end )

Left

str.slice( 0, len )

Mid

str.substr( i, len )

Right

str.slice( -len, str.length )

And so on... (should I continue?)

Use both Javascript basic methods and JQuery for DOM and moment.js for dates.

Read this: Utils library if you're looking for compatibility between browsers.

Or you can write your own Apache-like commons-lang too!

I'm constantly switching between Java backend and JavaScript frontend so for me it makes a lot of sense just to blindly use the StringUtils methods and don't even think about it. It would be great if someone would take the time to port all of the Apache StringUtils methods into a JavaScript ;-)

Here's my contribution:

  String.prototype.startsWith = function(prefix) {
    return this.indexOf(prefix,0) === 0;
  };

  String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
  };

  String.prototype.substringBefore = function(str) {
    var idx = this.indexOf(str);
    if( idx!==-1 ) {
      return this.substr(0,idx);
    }
    return this;
  };

  String.prototype.substringBeforeLast = function(str) {
    var idx = this.lastIndexOf(str);
    if( idx!==-1 ) {
      return this.substr(0,idx);
    }
    return this;
  };

  String.prototype.substringAfter = function(str) {
    var idx = this.indexOf(str);
    if( idx!==-1 ) {
      return this.substr(idx+str.length);
    }
    return this;
  };

  String.prototype.substringAfterLast = function(str) {
    var idx = this.lastIndexOf(str);
    if( idx!==-1 ) {
      return this.substr(idx+str.length);
    }
    return this;
  };

  // left pad with spaces (or the specified character) to this length 
  String.prototype.leftPad = function (length,c) {
    c = c || " ";
    if( length <= this.length ) return this;
    return new Array(length-this.length+1).join(c) + this;
  };

  // right pad with spaces (or the specified character) to this length 
  String.prototype.rightPad = function (length,c) {
    c = c || " ";
    if( length <= this.length ) return this;
    return this + new Array(length-this.length+1).join(c);
  };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top