Instance Methods - Gavin-Song/super.java GitHub Wiki

Remember: All String methods are also copied over (Any String method that previously returned String now returns SuperString)

after(String a)

Returns the substring after the first occurrence of a. If a is not present, returns the entire string. Example:

new SuperString("This class is cool").after("is "); // Returns "cool"
new SuperString("This class is cool").after("doesn't exist"); // Returns "This class is cool"

before(String a)

Returns the substring before the first occurrence of a. If a is not present, returns the entire string. Example:

new SuperString("This class is cool").before(" is"); // Returns "This class"
new SuperString("This class is cool").before("doesn't exist"); // Returns "This class is cool"

between(String a, String b)

Returns the substring between 2 strings. If either a or b is not present, will default to 0 and string.length, respectively, as indices for the substring. Example:

new SuperString("<p>Some text</p>").between("<a>", "</p>"); // Returns "Some text"
new SuperString("<p>Some text and more").between("<p>", "</p>"); // Returns "Some text and more"
new SuperString("Some text</p>").between("<p>", "</p>"); // Returns "Some text"

camelize()

Converts a string from underscore-case or dash-case to CamelCase (Class style). Example:

new SuperString("some_short-text").camelize(); // Returns "SomeShortText

capitalize()

Capitalizes the first letter, then lowercases the rest of the string (Ignores words). Example:

new SuperString("hEllO WoRlD")capitalize(); // Returns "Hello world"

chompLeft(String prefix)

If prefix is at the beginning of the string, remove it. Example:

new SuperString("/command").chompLeft("/"); // Returns "command"
new SuperString("command").chompLeft("/"); // Returns "command"

chompRight(String suffix)

If suffix is at the end of the string, remove it. Example:

new SuperString("cool!").chompRight("!"); // Returns "cool"
new SuperString("cool").chompRight("!"); // Returns "cool"

chunk(int chunks)

Chunks the string into chunks of size chunks. It is possible the last chunk could be shorter than the rest, if the string doesn't divide chunks evenly. Example:

new SuperString("hello world").chunk(2); // Returns {"he", "ll", "o ", "wo", "rl", "d"}}

chunkIntoNChunks(int n)

Chunks the string into chunks chunks. It is possible the last chunk could be shorter than the rest, if the string doesn't divide chunks evenly. Example:

new SuperString("hello world").chunkIntoNString(3); // Returns {"hell", "o wo", "rld"}

collapseWhitespace()

Removes duplicate whitespace in the string, replacing with a single space. Example:

new SuperString("test    test \n test").collapseWhitespaceString(); // Returns "test test test"

count(String substring, boolean allow_overlap)

Counts how many times substring appears in the string. If allow_overlap is true, then it will count overlapping matches. Example:

new SuperString("ababababa").count("aba"); // Returns 2, since it avoids overlaps
new SuperString("ababababa").count("aba", true); // Returns 4

dasherize()

Converts from camelCase to dashes. Example:

new SuperString("CamelCase Test").dasherize(); // Returns "camel-case test"

delete(int start_index, int end_index=start_index+1)

Returns the string with the portion from start_index to end_index deleted. If end_index is not provided, deletes just the character at start_index. Example:

new SuperString("hi").delete(0); // Returns "i"
new SuperString("hello").delete(0, 2); // Returns "llo"

ensureLeft(prefix)

Makes sure the left of the string starts with prefix. If it doesn't it prepends prefix to the string, otherwise returns the string unchanged. Example:

new SuperString("test").ensureLeft("/"); // Returns "/test"
new SuperString("/test").ensureLeft("/"); // Returns "/test"

ensureRight(suffix)

Makes sure the right of the string starts with suffix. If it doesn't it appends suffix to the string, otherwise returns the string unchanged. Example:

new SuperString("SuperString is cool").ensureRight("!"); // Returns "SuperString is cool!"
new SuperString("SuperString is cool!").ensureRight("!"); // Returns "SuperString is cool!"

getHashCode()

Returns the hashcode, like how System.out.print does for objects. Example:

new SuperString("something").getHashCode(); // Returns SuperString@1540e19d

getStringAt(int index)

Returns a String of length 1 representing the character at the index. Example:

new SuperString("test").getStringAt(1); // Returns "e"

getSuperStringAt(int index)

Returns a SuperString of length 1 representing the character at the index. Example:

new SuperString("test").getStringAt(1); // Returns "e"

includes(String substr)

Does substr occur in the string? Example:

new SuperString("hello world").includes("world"); // true
new SuperString("hello world").includes("apple"); // false

initals()

Takes the first letter of each word and returns the acronym made. Example:

new SuperString("Some sentence with spaces"); // Returns SSWS

insert(int index, String substr)

Inserts substr at the index, and returns the new string. Example:

new SuperString("word").insert(3, "l"); // Returns "world"

is24HourTime()

Is the string in a valid 24 time format? Example:

new SuperString("23:12").is24HourTime(); // true
new SuperString("2018").is24HourTime(); // false

isAfterAlphabetically(String s)

Is the string sorted after alphabetically compared to s? This method uses compareToIgnoreCase. If s equals the string, returns false. Example:

new SuperString("b").isAfterAlphabetically("a"); // true
new SuperString("a").isAfterAlphabetically("b"); // false
new SuperString("a").isAfterAlphabetically("a"); // false

isAlpha

Does the string only contain alphabetical characters? Example:

new SuperString("abc").isAlpha(); // true
new SuperString("abc123+").isAlpha(); // false

isAlphaNumeric

Does the string only contain alphanumeric characters? Example:

new SuperString("abc123").isAlphaNumeric(); // true
new SuperString("abc+123").isAlphaNumeric(); // false

isBeforeAlphabetically(String s)

Is the string sorted before alphabetically compared to s? This method uses compareToIgnoreCase. If s equals the string, returns false. Example:

new SuperString("a").isBeforeAlphabetically("b"); // true
new SuperString("b").isBeforeAlphabetically("a"); // false
new SuperString("a").isBeforeAlphabetically("a"); // false

isBlank()

This has an alias: isEmpty()

Is the string only composed of blank spaces (newlines, escapes, etc...) Example:

new SuperString("   \n").isBlank(); // true

isDDMMYYYY()

Does the string match the DD/MM/YYYY date format? Example:

new SuperString("25/12/1991").isDDMMYYYY(); // true
new SuperString("19-01-2038").isDDMMYYYY(); // true
new SuperString("12/31/1999").isDDMMYYYY(); // false, this is mm/dd/yyyy

isHexColorCode()

This has an alias: isHexColourCode()

Is the string a valid hex color code? Example:

new SuperString("#ABABAB").isHexColorCode(); // true
new SuperString("#FFF").isHexColorCode(); // true
new SuperString("#HHH").isHexColorCode(); // false

isISO8601Date()

Does the string follow the ISO-8601 date/time format? (See https://en.wikipedia.org/wiki/ISO_8601) Example:

new SuperString("2018-02-15").isISO8601Date(); // true
new SuperString("2018-02-15T23:30:09+00:00").isISO8601Date(); // true

isLower()

Is the string only composed of lowercase characters? Example:

new SuperString("hi123").isLower(); // true
new SuperString("Hi123").isLower(); // false

isMorseCode()

Is the string valid morse code? Only . and - are allowed (No slashes). Example:

new SuperString(".... . .-.. .-.. --- .-- --- .-. .-.. -..").isMorseCode(); // true
new SuperString("......").isMorseCode(); // false, not valid morse

isNumber()

Is the string a valid number? Only decimal numbers are supported, such as 1.3 and -2. Scientific notation and fractions are not supported. Example:

new SuperString("5").isNumber(); // true
new SuperString("-.2").isNumber(); // true
new SuperString("not a number").isNumber(); // false

isNumeric()

Does the string only contain numeric characters? Example:

new SuperString("123").isNumeric(); // true
new SuperString("abc").isNumeric(); // false

isUpper()

Is the string only made of uppercase characters? Example:

new SuperString("HI123").isUpper(); // true
new SuperString("Hi123").isUpper(); // false

isValidEmail()

Is the string a valid email? Very basic regex matching, just checks if it follows this pattern:

[some text]@[some text]

Example:

new SuperString("[email protected]").isValidEmail(); // true

isValidIpv4()

Is the string a valid ipv4 address? Example:

new SuperString("255.255.255.0").isValidIpv4(); // true

isValidIpv6()

Is the string a valid ipv6 address? Example:

new SuperString("2001:0db8:85a3:0000:0000:8a2e:0370:7334").isValidIpv6(); // true

isValidPhoneNumber()

Very basic phone number checking. Basically, does the string match this pattern:

[some numbers][maybe some random symbols][some numbers] etc...

Example:

new SuperString("123-456-7890").isValidPhoneNumber(); // true, yes...
new SuperString("1234567890").isValidPhoneNumber(); // true, yes...
new SuperString("123").isValidPhoneNumber(); // true, yes...
new SuperString("12").isValidPhoneNumber(); // false

isValidPostalCode()

Is the string a valid postal code? Only supports Canadian and American postal codes. Example:

new SuperString("10001").isValidPostalCode(); // true
new SuperString("A1A1A1").isValidPostalCode(); // true

isValidUrl()

Is the string a valid url? Urls can start with either http:, https:, ftp: or file:, although not required to. Example:

new SuperString("https://google.com").isValidUrl(); // true
new SuperString("test.test").isValidUrl(); // true
new SuperString("ftp://test.test").isValidUrl(); // true

isValidUsername()

Is the string a valid username? Usernames are alphanumeric and are between 3-16 characters long, inclusive. Example:

new SuperString("Superstring123").isValidUsername(); // true

isValidYoutubeUrl()

Is the string a valid youtube url? Example:

new SuperString("https://www.youtube.com/watch?v=dQw4w9WgXcQ").isValidYoutubeUrl(); // true
new SuperString("https://youtu.be/dQw4w9WgXcQ").isValidYoutubeUrl(); // true
new SuperString("https://youtu.be/dQw4w9WgXcQ?t=10").isValidYoutubeUrl(); // true

left(int n)

Returns the left n characters. If n < 0, returns the right n characters instead. n should be less than the length of the string, otherwise it might throw an IndexOutOfBounds exception. Example:

new SuperString("test").left(2); // "te"

lines()

Returns a SuperString[]

Splits the string along newlines, and returns the array. Supports windows and UNIX newlines. Example:

new SuperString("a\nb\nc").lines(); // {"a", "b", "c"}

linesString()

Returns a String[]

Same as lines(), but returns a String[] array instead of a SuperString[]

match(String regex)

Returns an ArrayList

Returns all groups in the string that match the regex. Example:

new SuperString("1 2 3").match("\d"); // ["1", "2", "3"]

matchString(String regex)

Returns an ArrayList

Same as match, but returns an ArrayList instead.

pad(int target_length, String pad_char=" ")

Pads the string to target_length with pad_char. If the string cannot be padded evenly on both sides, the beginning takes priority. Example:

new SuperString("test").pad(6); // " test "
new SuperString("test").pad(6, "-"); // "-test-"
new SuperString("test").pad(7); // "  test "

Note: If pad_char has a length greater than 1, the resulting string could have a greater length than the parameter.

padLeft(int target_length, String pad_char=" ")

Pads the string to target_length with pad_char, padding to the left. Example:

new SuperString("test").padLeft(5); // " test"
new SuperString("test").padLeft(5, "-"); // "-test"

Note: If pad_char has a length greater than 1, the resulting string could have a greater length than the parameter.

padRight(int target_length, String pad_char=" ")

Pads the string to target_length with pad_char, padding to the right. Example:

new SuperString("test").padRight(5); // "test "
new SuperString("test").padRight(5, "-"); // "test-"

Note: If pad_char has a length greater than 1, the resulting string could have a greater length than the parameter.

repeat(int n)

Repeats the string n times. Example:

new SuperString("hi").repeat(3); // "hihihi"

reverse()

Reverses the string. Example:

new SuperString("test").reverse(); // "tset"

right(int n)

Takes the right n characters. If n < 0, takes the left n instead. n should be less than the length of the string, otherwise it might throw an IndexOutOfBounds exception. Example:

new SuperString("test").right(2); // "st"

rotate(int offset)

Rotates the string by offset. Values are rotated going to the left. If a negative offset is passed, it will rotate to the right instead. Example:

new SuperString("hello world").rotateString(1) // "ello worldh"

shift(int offset, boolean only_shift_alpha=true)

Shifts each letter by offset.

  • If only_shift_alpha is true, then it acts like a Caesar cipher, shifting each letter up offset places. All letters become uppercase, and non-alphabetical characters remain unchanged.
  • Otherwise, shifts each character up by their ASCII character code.

Example:

new SuperString("attack at dawn").shiftString(2); // "CVVCEM CV FCYP"
new SuperString("attack at dawn").shiftString(2, false); // "cvvcem\"cv\"fcyp"

splice(int index, int length, String replacement)

Replaces the portion of the string starting from index of length length with replacement. Same as JS array splicing. Example:

new SuperString("hello world").splice(0, 5, "goodbye"); // "goodbye world"
new SuperString("hello word").splice(9, 0, "l"); // "hello world"

splitFromRight(String s, int limit=infinity)

Splits from the right of the string, by string s. (Note that s is NOT a regex). Limit is the max number of times it will split by s, in other words, the resulting array has a length of at most limit + 1. Example:

new SuperString("a.b.c.d.e").splitFromRight("."); // {"e", "d", "c", "b", "a"}
new SuperString("a.b.c.d.e").splitFromRight(".", 2); // {"e", "d", "a.b.c"}

WARNING Internally splitFromRight creates a copy of the array - if your array is very large, you might suffer intense memory usage. (Should have no issues with small arrays)

strip()

Functions the same as String.trim() (Removes leading/trailing whitespace)

strip(char... args)

strip(String... args)

strip(SuperString... args)

Removes all occurrences of args from the string. You can either pass args as an array, or as individual arguments (Ie strip({"a", "b"}) vs strip("a", "b"). Example:

new SuperString("abc").strip("a", "b"); // "c"

stripLeft(char... args)

stripLeft(String... args)

stripLeft(SuperString... args)

Removes all occurrences of args from the string, but only if the string starts with it. You can either pass args as an array, or as individual arguments (Ie strip({"a", "b"}) vs strip("a", "b"). Example:

new SuperString("abc").stripLeft("a", "b"); // "bc"

stripRight(char... args)

stripRight(String... args)

stripRight(SuperString... args)

Removes all occurrences of args from the string, but only if the string ends with it. You can either pass args as an array, or as individual arguments (Ie strip({"a", "b"}) vs strip("a", "b"). Example:

new SuperString("abc").stripRight("b", "c"); // "ab"

stripPunctuation()

Removes all punctuation from the string. Example:

new SuperString("This is so cool! Amazing.").stripPunctuation(); // "This is so cool Amazing"

substr(int index, int length)

Similar to Javascript's substr, however it can throw an IndexOutOfBoundsException if the index is not in range of the string (However, index can be negative, in which it will count backwards from the string)

Removes the portion of length length starting at index

Example:

new SuperString("hello world").substr(1, 4); // "ello".

swapCase()

Inverts the case. Example:

new SuperString("Hello").swapCase(); // "hELLO"

titleCase()

Different from capitalize. Capitalizes the first letter of each word (While capitalize only does so for entire string). Example:

new SuperString("heLLO WoRld").titleCase(); // "Hello World"

toBigDecimal()

Converts to BigDecimal Object using BigDecimal's constructor. Example:

new SuperString("5.5").toBigDecimal(); // [BigDecimal Object]
new SuperString("not a number").toBigDecimal(); // Errors

toBool()

Converts to a boolean. The strings yes, true, y and 1 convert to true (Case insensitive), any other string returns false. Example:

new SuperString("true").toBool(); // true
new SuperString("YES").toBool(); // true
new SuperString("asdsada").toBool(); // false

toDouble()

Converts string to double, using Double.parseDouble

toInt()

Converts to int, using Integer.parseInt

toFloat()

Converts to float, using Float.parseFloat

toLong()

Converts to long, using Long.parseLong

toStringArray()

Returns a String[]

Returns a String[] where each element is a character in the String. Example:

new SuperString("hello").toStringArray(); // {"h", "e", "l", "l", "o"}

toSuperStringArray()

Returns a SuperString[]

Same as toStringArray(), but returns a SuperString[]

trim()

Removes whitespace from the beginning and end of the string. Example:

new SuperString("   trim    \n").trim(); // "trim"

trimLeft()

Removes whitespace from the beginning of the string. Example:

new SuperString("   trim    \n").trimLeft(); // "trim    \n"

trimRight()

Removes whitespace from the end of the string. Example:

new SuperString("   trim    \n").trimRight(); // "   trim"

truncate(int length, String suffix="...")

Shortens the string to length (Including suffix in the length), and adds suffix to the end of the string. If the string is already short enough, returns it unchanged. Example:

new SuperString("some long string blah blah").truncate(10); // "some lon..."
new SuperString("short").truncate(10); // "short"

uncapitalize()

Makes the first letter of the string lowercase. Example:

new SuperString("HeLLO").uncapitalize(); // "heLLO"

underscore()

Converts from CamelCase to underscore. Example:

new SuperString("CamelCase").underscore(); // "camel_case"

wrap(int line_length, boolean allow_word_break=true)

Adds newlines into the string so that each line is line_length long. If allow_word_break is true, then it will break between words, if disabled it will not break between words at all, no matter how long. Example:

new SuperString("this is some very long text").wrap(10);
/* Output:
this is so
me very lo
ng text */

new SuperString("this is some very long text").wrap(10, false);
/* Output:
this is some
very long text */
⚠️ **GitHub.com Fallback** ⚠️