There is no person who would not know the String class. To facilitate the work of this class, has been created ‘Strings’. We have created such methods as : join, numberOfOccurrences, removeAllOccurrences, indexesOf, groups, random, pad, capitalize, capitalizeAllWords, isWhitespace, containsOnly, isEmpty, singleLine, removeNewLines, reverse, repeat, isAlpha, isAlphaWithWhitespace, isNumeric, isAlphanumeric, isAlphanumericWithWhitespace
join
There are two methods responsible for created String from given collection or array of object:
For each object from collection is invoked toString() method. If given object is null, to resulting string is added the ‘null’ value.
Examples
Number of occurrences
To check the number of occurrences of a sequence in the String you can use one of the following methods:
As you can see, each method has its own version of ignoring the regular expression. Sometimes you want to search of dots, but what happens when you directly pass the “.” as parameter ? Will be returned all the characters, because dot represents any char. Fortunately, you can easily fix this now. Look at the following example:
Remove all occurrences
This method is very similar to the previous method and also has four variants.
Indexes of
If you want to get all of the indexes given occurrences, this method is right for you.
Groups
This method is very similar to the above, but there’s returned a more detailed information.
Random
Have you ever tried to look for a method that would generate a random string for you? That’s right, there is no such method! Fortunately sweetener provides two such methods:
The first one generates a random string of: a-z A-Z 0-9
Whereas the second takes a list of characters used in generation.
Pad
The default fill character (character c or string content) is a space.
PaddingType property determines whether the padding should be on the left, right or central - the default is right.
Capitalize
Is whitespace
Contains only
This method returns an indication whether the specified string consists exclusively of characters passed as the second argument.
Is empty
This method returns true if value is null, empty or consists of only whitespaces.
Single line
This method removes all occurrences of characters: \n \r. As a result returned is a single line.
Remove new lines
This method removes the newline at the end of the String.
Reverse
This method reverses the string.
Repeat
This method repeat given sequence specified number of times.
Is alpha, alphaWithWhitespace, numeric, alphanumeric, alphanumericWithWhitespace