Skip to content

Boon String Slicing

RichardHightower edited this page Feb 15, 2014 · 6 revisions

Boon Home | Boon Source | If you are new to boon, you might want to start here. Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity. A real boon to Java to developers!

Java Boon - Java String Slicing

Many languages have slice notation (Ruby, Groovy and Python). Boon adds this to Java Strings

Here are some examples of Boon Java String Slicing

        String letters = "abcd";

        boolean worked = true;

        worked &=

                idx(letters, 0)  == 'a'
                        || die("0 index is equal to a");



        worked &=

                idx(letters, -1)  == 'd'
                        || die("-1 index is equal to a");

Another way to express idx(letters, -1) == 'd' is idx(letters, letters.length() - 1) == 'd'!

I prefer the shorter way!

        worked &=

                idx(letters, letters.length() - 1) == 'd'
                         || die("another way to express what the -1 means");


        //We can modify too
        letters = idx(letters, 1, 'z');

        worked &=

                idx(letters, 1) == 'z'
                        || die("Set the 1 index of letters to 'z'");


        worked &= (
                in('a', letters) &&
                in('z', letters)
        ) || die("'z' is in letters and 'a' is in letters");

Slice Slice Baby!

        letters = "abcd";

        worked &=
                slc(letters, 0, 2).equals("ab")
                    || die("index 0 through index 2 is equal to 'ab'");



        worked &=
                slc(letters, 1, -1).equals("bc")
                        || die("index 1 through index (length -1) is equal to 'bc'");


        worked &=
                slcEnd(letters, -2).equals("ab")
                        || die("Slice of the end of the string!");


        worked &=
                slcEnd(letters, 2).equals("ab")
                        || die("Vanilla Slice Slice baby!");

Thoughts

Thoughts? Write me at richard high tower AT g mail dot c-o-m (Rick Hightower).

Further Reading:

If you are new to boon start here:

Why Boon?

Easily read in files into lines or a giant string with one method call. Works with files, URLs, class-path, etc. Boon IO support will surprise you how easy it is. Boon has Slice notation for dealing with Strings, Lists, primitive arrays, Tree Maps, etc. If you are from Groovy land, Ruby land, Python land, or whatever land, and you have to use Java then Boon might give you some relief from API bloat. If you are like me, and you like to use Java, then Boon is for you too. Boon lets Java be Java, but adds the missing productive APIs from Python, Ruby, and Groovy. Boon may not be Ruby or Groovy, but its a real Boon to Java development.

Core Boon Philosophy

Core Boon will never have any dependencies. It will always be able to run as a single jar. This is not just NIH, but it is partly. My view of what Java needs is more inline with what Python, Ruby and Groovy provide. Boon is an addition on top of the JVM to make up the difference between the harder to use APIs that come with Java and the types of utilities that are built into Ruby, Python, PHP, Groovy etc. Boon is a Java centric view of those libs. The vision of Boon and the current implementation is really far apart.

===

Contact Info

blog|[twitter](https://twitter.com/RickHigh|[infoq]http://www.infoq.com/author/Rick-Hightower|[stackoverflow](http://stackoverflow.com/users/2876739/rickhigh)|[java lobby](http://java.dzone.com/users/rhightower)|Other | richard high tower AT g mail dot c-o-m (Rick Hightower)|work|cloud|nosql

Clone this wiki locally