Constant String is a library for doing constant time string manipulation in Java.
CString cs = CString.create("foo")
.concat(CString.create("bar"))
.substring(3)
.concat(CString.create("bara"))
.delete(3,7)
.insert(0, CString.create("foo"));
cs.toString(); // foobar
Perhaps not the most useful thing in everyday programming, but still a fun exercise to implement.
The artifacts are published on Maven Central, so trying it out is as simple as adding
the dependency to you pom.xml
or gradle file.
<dependency>
<groupId>us.klette</groupId>
<artifactId>constantstring</artifactId>
<version>2.0</version>
</dependency>
Constant String exposes a simple API via the CString-interface. The static method CString.create(...)
allows you to quickly create a new instance of a (hidden) implementation of the interface. Some of the operations are similar to the ones found on the built-in Java String class, such as the substring method. On these we try hard to map the arguments and functionality as close as possible.
The following operations are supported by the API.
The toString()
-method evaluates the graph of operations, and returns the final result as a String. This is done
in a non-constant time.
Create a new CString instance from a String.
CString myStr = CString.create("The start of my text goes like this..")
Extracts a part of a String, either only by start index to the end of the string, or by a start index and an end index.
myStr.substring(4).toString() // "start of my text goes like this.."
myStr.substring(4,8).toString() // "start"
Appends a String at the end of the current value.
CString.create("foo")
.concat(CString.create("ba"))
.concat("r")
.toString() // "foobar"
Delete part of a string specified by a start index and an end index.
CString.create("foobar").delete(3,6).toString() // "foo"
Inserts a String at a given index.
CString.create("foar").insert("o",2).insert(CString.create("b"), 3).toString() // "foobar"