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)
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"
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"
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"
Converts a string from underscore-case or dash-case to CamelCase (Class style). Example:
new SuperString("some_short-text").camelize(); // Returns "SomeShortText
Capitalizes the first letter, then lowercases the rest of the string (Ignores words). Example:
new SuperString("hEllO WoRlD")capitalize(); // Returns "Hello world"
If prefix is at the beginning of the string, remove it. Example:
new SuperString("/command").chompLeft("/"); // Returns "command"
new SuperString("command").chompLeft("/"); // Returns "command"
If suffix is at the end of the string, remove it. Example:
new SuperString("cool!").chompRight("!"); // Returns "cool"
new SuperString("cool").chompRight("!"); // Returns "cool"
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"}}
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"}
Removes duplicate whitespace in the string, replacing with a single space. Example:
new SuperString("test test \n test").collapseWhitespaceString(); // Returns "test test test"
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
Converts from camelCase to dashes. Example:
new SuperString("CamelCase Test").dasherize(); // Returns "camel-case test"
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"
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"
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!"
Returns the hashcode, like how System.out.print does for objects. Example:
new SuperString("something").getHashCode(); // Returns SuperString@1540e19d
Returns a String of length 1 representing the character at the index. Example:
new SuperString("test").getStringAt(1); // Returns "e"
Returns a SuperString of length 1 representing the character at the index. Example:
new SuperString("test").getStringAt(1); // Returns "e"
Does substr occur in the string? Example:
new SuperString("hello world").includes("world"); // true
new SuperString("hello world").includes("apple"); // false
Takes the first letter of each word and returns the acronym made. Example:
new SuperString("Some sentence with spaces"); // Returns SSWS
Inserts substr at the index, and returns the new string. Example:
new SuperString("word").insert(3, "l"); // Returns "world"
Is the string in a valid 24 time format? Example:
new SuperString("23:12").is24HourTime(); // true
new SuperString("2018").is24HourTime(); // false
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
Does the string only contain alphabetical characters? Example:
new SuperString("abc").isAlpha(); // true
new SuperString("abc123+").isAlpha(); // false
Does the string only contain alphanumeric characters? Example:
new SuperString("abc123").isAlphaNumeric(); // true
new SuperString("abc+123").isAlphaNumeric(); // false
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
This has an alias: isEmpty()
Is the string only composed of blank spaces (newlines, escapes, etc...) Example:
new SuperString(" \n").isBlank(); // true
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
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
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
Is the string only composed of lowercase characters? Example:
new SuperString("hi123").isLower(); // true
new SuperString("Hi123").isLower(); // false
Is the string valid morse code? Only . and - are allowed (No slashes). Example:
new SuperString(".... . .-.. .-.. --- .-- --- .-. .-.. -..").isMorseCode(); // true
new SuperString("......").isMorseCode(); // false, not valid morse
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
Does the string only contain numeric characters? Example:
new SuperString("123").isNumeric(); // true
new SuperString("abc").isNumeric(); // false
Is the string only made of uppercase characters? Example:
new SuperString("HI123").isUpper(); // true
new SuperString("Hi123").isUpper(); // false
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
Is the string a valid ipv4 address? Example:
new SuperString("255.255.255.0").isValidIpv4(); // true
Is the string a valid ipv6 address? Example:
new SuperString("2001:0db8:85a3:0000:0000:8a2e:0370:7334").isValidIpv6(); // true
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
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
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
Is the string a valid username? Usernames are alphanumeric and are between 3-16 characters long, inclusive. Example:
new SuperString("Superstring123").isValidUsername(); // true
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
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"
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"}
Returns a String[]
Same as lines(), but returns a String[] array instead of a SuperString[]
Returns an ArrayList
Returns all groups in the string that match the regex. Example:
new SuperString("1 2 3").match("\d"); // ["1", "2", "3"]
Returns an ArrayList
Same as match, but returns an ArrayList instead.
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.
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.
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.
Repeats the string n times. Example:
new SuperString("hi").repeat(3); // "hihihi"
Reverses the string. Example:
new SuperString("test").reverse(); // "tset"
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"
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"
Shifts each letter by offset.
- If
only_shift_alpha
is true, then it acts like a Caesar cipher, shifting each letter upoffset
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"
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"
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)
Functions the same as String.trim() (Removes leading/trailing whitespace)
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"
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"
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"
Removes all punctuation from the string. Example:
new SuperString("This is so cool! Amazing.").stripPunctuation(); // "This is so cool Amazing"
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".
Inverts the case. Example:
new SuperString("Hello").swapCase(); // "hELLO"
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"
Converts to BigDecimal Object using BigDecimal's constructor. Example:
new SuperString("5.5").toBigDecimal(); // [BigDecimal Object]
new SuperString("not a number").toBigDecimal(); // Errors
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
Converts string to double, using Double.parseDouble
Converts to int, using Integer.parseInt
Converts to float, using Float.parseFloat
Converts to long, using Long.parseLong
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"}
Returns a SuperString[]
Same as toStringArray(), but returns a SuperString[]
Removes whitespace from the beginning and end of the string. Example:
new SuperString(" trim \n").trim(); // "trim"
Removes whitespace from the beginning of the string. Example:
new SuperString(" trim \n").trimLeft(); // "trim \n"
Removes whitespace from the end of the string. Example:
new SuperString(" trim \n").trimRight(); // " trim"
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"
Makes the first letter of the string lowercase. Example:
new SuperString("HeLLO").uncapitalize(); // "heLLO"
Converts from CamelCase to underscore. Example:
new SuperString("CamelCase").underscore(); // "camel_case"
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 */