Skip to content

HarryStevens/-DEPRECATED-strings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project is deprecated. Please use jsz instead.

strings.js

strings.js is a Javascript library for manipulating strings. Javascript has many built-in methods for manipulating strings. The functions in this library are meant to supplement those.

To use this library, download strings.js or strings.min.js from the src directory and include it in your HTML file as <script src="path/to/strings.js"></script> or <script src="path/to/strings.min.js"></script>.

API Reference

Functions for changing the case of words and characters.

# strings.toCamelCase(string)

Transforms a string into camel case.

strings.toCamelCase("Hello world!"); // "helloWorld"

# strings.toSentenceCase(string)

Capitalizes the first letter of the first word in a string.

# strings.toSlugCase(string)

Transforms a string into a slug.

strings.toSlugCase("Hello world!"); // "hello-world"

# strings.toSnakeCase(string)

Transforms a string into snake case.

strings.toSnakeCase("Hello world!"); // "hello_world"

# strings.toStartCase(string)

Capitalizes the first letter of every word in a string.

# strings.toTitleCase(string[, array, boolean])

Transforms a string into title case, where the first letter of every word is capitalized except for certain prepositions, articles and conjunctions.

strings.toTitleCase("the quick brown fox jumps over the lazy dog"); // "The Quick Brown Fox Jumps over the Lazy Dog"
strings.toTitleCase("javascript: a beginner's guide to the language of the web"); // Javascript: A Beginner's Guide to the Language of the Web
strings.toTitleCase("james comey to remain on as FBI director"); // "James Comey to Remain on as FBI Director"
strings.toTitleCase("new rules grant FBI, DEA & CIA access to raw NSA surveillance data"); // New Rules Grant FBI, DEA & CIA Access to Raw NSA Surveillance Data

Functions for manipulating numbers as strings.

# strings.numberCommas(string)

Adds commas to a number string for thousands, millions, billions, and so on.

# strings.numberDecimals(string, number)

Rounds a number string, both float and integer, to the nearest specified decimal place.

strings.numberDecimals("1", 2); // "1.00"
strings.numberDecimals("1.235", 2); // "1.24"

# strings.numberLakhs(string)

Adds commas to a number string for thousands, lakhs, crores, and so on. This is according to the Indian numbering system.

# strings.numberPrependZeros(string, number)

Adds zeros before a string so that the length of the string equals a given number of characters. Does nothing to the string if it is already longer than the number of characters.

strings.numberPrependZeros("1234", 6); // "001234"

Functions for testing strings for certain properties. Will return booleans.

# strings.endsWith(string, substring[, boolean])

Tests whether a string ends with a substring. Defaults to case sensitive, but you can set the third argument to true for case insensitive.

strings.endsWith("Hello world", "LD"); // false
strings.endsWith("Hello world", "LD", true); // true
strings.endsWith("Hello world", "LD", false); // false

# strings.includes(string, string[, boolean])

Tests whether a string includes a substring. Defaults to case sensitive, but you can set the third argument to true for case insensitive.

strings.includes("Hello world", "WO"); // false
strings.includes("Hello world", "WO", true); // true
strings.includes("Hello world", "WO", false); // false

# strings.isAllCaps(string)

Tests whether a string contains only capital letters. Ignores symbols and numbers.

# strings.isAllDigits(string)

Tests whether a string contains only digits.

# strings.isAllLower(string)

Tests whether a string contains only lowercase letters. Ignores symbols and numbers.

# strings.startsWith(string, substring[, boolean])

Tests whether a string starts with a substring. Defaults to case sensitive, but you can set the third argument to true for case insensitive.

strings.startsWith("Hello world", "he"); // false
strings.startsWith("Hello world", "he", true); // true
strings.startsWith("Hello world", "he", false); // false

Functions for applying various transformations to strings.

# strings.keepAll(string, substring)

Keeps all instances of a substring in a string. Removes everything else. Returns a blank string if the substring does not appear in the original string.

# strings.keepEnd(string, number)

Keeps a certain number of characters at the end of a string and removes the rest.

# strings.keepOne(string, string)

Keeps one instance of a substring in a string, even if the substring appears multiple times. Removes everything else. Returns a blank string if the substring does not appear in the original string.

# strings.keepStart(string, number)

Keeps a certain number of characters at the start of a string and removes the rest.

# strings.removeAll(string, substring)

Removes all instances of a substring from a string.

# strings.removeDigits(string)

Removes all digits from a string.

strings.removeDigits("H1e2l3lo w45orld!6"); // Hello world!

# strings.removeFirst(string, substring)

Removes the first instance of a substring from a string.

# strings.removeLast(string, substring)

Removes the last instance of a substring from a string.

# strings.removeSymbols(string)

Removes anything that is not a letter or a space from a string.

# strings.removeTags(string[, array])

Removes HTML tags from a string. You can pass an optional array with the tags you want to keep.

strings.removeTags("<span><i>Hello</i> <b>world</b>!</span>"); // Hello world!
strings.removeTags("<span><i>Hello</i> <b>world</b>!</span>", ["i", "span"]); // <span><i>Hello</i> world!</span>

# strings.replaceAll(string, substringA, substringB)

Replaces all instances of substring A in a string with substring B.

strings.replaceAll("Hello world!", "l", "z"); // Hezzo worzd!

# strings.replaceAt(string, index, replacement)

Replaces the characters of a string with another string beginning at a specific index.

strings.replaceAt("Hello world!", 6, "Jonny"); // Hello Jonny!

# strings.replaceFirst(string, substringA, substringB)

Replaces the first instance of substring A in a string with substring B. Uses the same functionality as the native Javascript String.replace() method.

strings.replaceFirst("Hello world!", "l", "z"); // Hezlo world!
strings.replaceFirst("Hello world!", "ll", "zz"); // Hezzo world!

# strings.replaceLast(string, substringA, substringB)

Replaces the last instance substring A in a string with substring B.

strings.replaceLast("Hello world!", "l", "z"); // Hello worzd!

# strings.reverseCharacters(string)

Reverses the order of a string's characters.

# strings.reverseWords(string)

Reverses the order of a string's words.

# strings.shuffleCharacters(string)

Randomly shuffles a string's characters. Uses the Fischer-Yates shuffle.

# strings.shuffleCharactersInWords(string)

Randomly shuffles the characters of each word, but keeps the words in order. Uses the Fischer-Yates shuffle.

# strings.shuffleWords(string)

Randomly shuffles a string's words. Uses the Fischer-Yates shuffle.

# strings.count(string, substring)

Counts the number of times a substring occurs in a string.

About

A Javascript library for manipulating strings.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published