SuperString - Gavin-Song/super.java GitHub Wiki
SuperString
Duplicates and extends the default functionality of a string. Many common methods are implemented, for ease of use. To create a SuperString, simply do
SuperString s = new SuperString("Some string here");
All String methods are replicated in SuperString, along with a ton of new ones. All methods that have a String parameter are also overloaded to accept SuperString. All methods that normally return a String in the String class now return a SuperString (ie replace). If you want to use a string instead, simply add "String" to the method name. Example:
SuperString s = new SuperString("Hello world!");
SuperString r = new SuperString("world");
// Note that it can accept both SuperString and String as a parameter
// Note that it returns SuperString, not String
SuperString replaced = s.replace(r, "");
// Adding a "String" to the method name returns a String
String replaced_str = s.replaceString(r, "");
// This also applies to methods that return String[]
- Note 1: I will not repeat overridden methods, or the String variant of methods in the documentation.
- Note 2: SuperString is immutable. All methods return a new SuperString as a result.
Cool things you can do
A code example is worth a thousand words.
Iterate over the SuperString
SuperString s = new SuperString("hello world");
for(SuperString ch: s){
System.out.println(s); // Prints each letter
}
Substring with negative index
Counts backwards, like in python. Should also work on most methods that have an index parameter.
new SuperString("abcde").substring(0, -2); // "abc"
Get the length without using .length()
int length = new SuperString("hi").length; // 2
Access inner String directly
String s = new SuperString("s").str; // "s"
(Don't worry about the last 2 - the instance variables are final and cannot be changed accidentally)