StringUtils in javascript

javascript string methods
javascript substring
lodash
javascript string replace
javascript split
javascript replaceall
javascript remove from string
javascript search

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.


String utils - Underscore.string

Object/array utils - Underscore

Date utils - Moment.js

JavaScript String Reference, ####A JavaScript clone of Apache's Java StringUtils, implemented using TypeScript. Methods that have been implemented currently are: isEmpty; isNotEmpty  The StringUtils class defines certain words related to String handling. null - null; empty - a zero-length string ("") space - the space character (' ', char 32) whitespace - the characters defined by Character.isWhitespace(char) trim - the characters <= 32 as in String.trim() StringUtils handles null input Strings quietly.


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?)

tejzpr/stringutils: A JavaScript clone of Apache's Java , string-utils.js. // String utils. //. // resources: // -- mout, https://github.com/mout/mout/​tree/master/src/string. /**. * "Safer" String.toLowerCase(). */. Unescapes any JavaScript literals found in the String to a Writer. For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'. A null string input has no effect.


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!

Useful collection of JavaScript string utilities. · GitHub, Voca provides functions to manipulate strings in JavaScript: change case, trim, pad, slugifly, latinise, sprintf, truncate, escape and more. The StringUtils class defines certain words related to String handling. null - null; empty - a zero-length string ("") space - the space character (' ', char 32) whitespace - the characters defined by Character.isWhitespace(char) trim - the characters <= 32 as in String.trim() StringUtils handles null input Strings quietly.


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

Voca: The JavaScript string library, The StringUtils class defines certain words related to String handling. null - null; empty - a zero-length string ( "" ); space - the space character ( '  <%= StringEscapeUtils.escapeHtml("cool 4' product") %> In view page source, the result is: JSP passing String attribute with XML characters to JavaScript function


StringUtils (Apache Commons Lang 3.6 API), Need private packages and team management tools?Check out npm Teams ». js​-string-utils. 0.0.3 • Public • Published 2 years ago. Readme · ExploreBETA · 0  substr () Extracts the characters from a string, beginning at a specified start position, and through the specified number of character. substring () Extracts the characters from a string, between two specified indices. toLocaleLowerCase () Converts a string to lowercase letters, according to the host's locale.


js-string-utils, Description. Basic library for javascript/typescript. @beautiful-code/string-utils Description. A port of inflection-js to node.js module  StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty. From the linked documentation: Checks if a String is whitespace, empty ("") or null.


string-utils, [code lang=”javascript”] String name = null; if(StringUtils.isEmpty(name)) { System​.out.println(“Its null”); // print Its null } [/code]. 2) isBlank()  java.lang does not contain a class called StringUtils. Several third-party libs do, such as Apache Commons Lang or the Spring framework . Make sure you have the relevant jar in your project classpath and import the correct class.