-
Notifications
You must be signed in to change notification settings - Fork 0
SuperString
Gavin-Song edited this page Feb 18, 2018
·
18 revisions
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.
A code example is worth a thousand words.
SuperString s = new SuperString("hello world");
for(SuperString ch: s){
System.out.println(s); // Prints each letter
}Counts backwards, like in python. Should also work on most methods that have an index parameter.
new SuperString("abcde").substring(0, -2); // "abc"int length = new SuperString("hi").length; // 2String s = new SuperString("s").str; // "s"(Don't worry about the last 2 - the instance variables are final and cannot be changed accidentally)