Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringUtils in javascript [closed]

Tags:

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.

like image 826
Freewind Avatar asked Mar 25 '12 14:03

Freewind


People also ask

What does StringUtils empty return?

isEmpty() is a static method of the StringUtils class that is used to check if a given string is empty or not. If a string satisfies any of the criteria below, then the string is considered to be empty. The length of the string is zero. The string points to a null reference.

What is string [] JavaScript?

A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Strings in JavaScript are primitive data types and immutable, which means they are unchanging.

How do you check a string is not empty in JS?

Use the length property to check if a string is empty, e.g. if (str. length === 0) {} . If the string's length is equal to 0 , then it's empty, otherwise it isn't empty. Copied!

Does StringUtils isEmpty check for NULL?

isEmpty(String) , which checks if a string is an empty string or null. It also provides the StringUtils. isBlank(String) method, which also checks for whitespace.


2 Answers

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

like image 31
Šime Vidas Avatar answered Oct 08 '22 07:10

Šime Vidas


String utils - Underscore.string

Object/array utils - Underscore

Date utils - Moment.js

like image 159
timrwood Avatar answered Oct 08 '22 07:10

timrwood