String Wrapper
StringWrapper¶
Provides utility methods for manipulating a String. On the following methods, wrap is a StringWrapper.
Reading¶
get¶
Retrieves the wrapped String
Syntax
String wrap.get()
Example
final String value = p6.wrap.of 'example' get()
assert value == 'example'
toString¶
Retrieves the String representation of the wrapped value.
Syntax
String wrap.toString()
Example
final s = p6.wrap.of 'example' toString()
final String s = p6.wrap.of 'example'
Conversions¶
asNumber¶
Converts the string to a number.
Syntax
NumberWrapper wrap.asNumber()
Example
final value = p6.wrap.of '123' asNumber() get()
assert value == 123
asDate¶
Converts the string to a date using the default format.
Default: Date format = yyyy-MM-dd'T'HH:mm:ss.SSSZ
Syntax
DateWrapper wrap.asDate()
Example
final String value = p6.wrap.of '2023-10-01T12:34:56.789+0100' asDate() get()
assert value == 'Sun Oct 01 11:34:56 UTC 2023'
asDate (with format)¶
Converts the string to a date using the specified format.
Syntax
DateWrapper wrap.asDate(String format)
Example
final String value = p6.wrap.of '01-10-2023' asDate 'dd-MM-yyyy' get()
assert value == 'Sun Oct 01 00:00:00 UTC 2023'
asFile¶
Converts the string to a file.
Note
The file path will be resolved using the file or p6file protocol. If the protocol is not present, the path will be resolved as a local file.
Syntax
FileWrapper wrap.asFile()
Example
final String value = p6.wrap.of 'file:///opt/path/to' asFile() get()
assert value == '/opt/path/to'
final String value = p6.wrap.of 'p6file:///${P6_DATA}/path/to' asFile() get()
assert value == '/opt/p6core.data/path/to'
final String value = p6.wrap.of '/opt/path/to' asFile() get()
assert value == '/opt/path/to'
asXml¶
New Feature
Since 6.10.15
Converts the string to a Xml.
Syntax
XmlWrapper wrap.asXml()
Example
final String value = p6.wrap.of '<root><fee>Fee</fee></root>' asXml() get()
assert value == 'Fee'
asJson¶
New Feature
Since 6.10.24
Parses the string as JSON into a Map/List tree (companion to asXml). Navigate the
result with native Groovy (GPath).
Syntax
Object wrap.asJson()
Example
final json = p6.wrap.of '{"a":1}' asJson()
assert json.a == 1
asBoolean¶
Returns true if the wrapped String is not null and not empty.
Syntax
boolean wrap.asBoolean()
Example
assert p6.wrap.of('example').asBoolean() == true
assert p6.wrap.of('').asBoolean() == false
Case¶
upper¶
Converts the string to uppercase.
Syntax
StringWrapper wrap.upper()
Example
final String value = p6.wrap.of 'example' upper()
assert value == 'EXAMPLE'
lower¶
Converts the string to lowercase.
Syntax
StringWrapper wrap.lower()
Example
final String value = p6.wrap.of 'EXAMPLE' lower()
assert value == 'example'
capitalize¶
Capitalizes the first character of the string.
Syntax
StringWrapper wrap.capitalize()
Example
final String value = p6.wrap.of 'example' capitalize()
assert value == 'Example'
uncapitalize¶
Uncapitalizes the first character of the string.
Syntax
StringWrapper wrap.uncapitalize()
Example
final String value = p6.wrap.of 'Example' uncapitalize()
assert value == 'example'
camelCase / snakeCase / kebabCase¶
New Feature
Since 6.10.24
Converts between naming conventions (splitting on spaces, _, - and camel humps).
Syntax
StringWrapper wrap.camelCase()
StringWrapper wrap.snakeCase()
StringWrapper wrap.kebabCase()
Example
assert p6.wrap.of('hello world').camelCase() == 'helloWorld'
assert p6.wrap.of('helloWorld').snakeCase() == 'hello_world'
assert p6.wrap.of('Hello World').kebabCase() == 'hello-world'
slugify¶
New Feature
Since 6.10.24
Lowercases and replaces non-alphanumeric runs with - (URL-friendly slug).
Syntax
StringWrapper wrap.slugify()
Example
assert p6.wrap.of('Hello, World! 42').slugify() == 'hello-world-42'
Trimming & padding¶
trim¶
Trims whitespace from both ends of the string.
Syntax
StringWrapper wrap.trim()
Example
final String value = p6.wrap.of ' example ' trim()
assert value == 'example'
strip¶
New Feature
Since 6.10.24
Strips whitespace from both ends of the string.
Syntax
StringWrapper wrap.strip()
Example
final value = p6.wrap.of ' hi ' strip()
assert value == 'hi'
stripStart¶
Removes the leading characters that match the specified sequence.
Syntax
StringWrapper wrap.stripStart(String seq)
Example
final String value = p6.wrap.of 'example' stripStart 'ex'
assert value == 'ample'
stripEnd¶
New Feature
Since 6.10.24
Removes the trailing characters that match the specified sequence.
Syntax
StringWrapper wrap.stripEnd(String seq)
Example
final value = p6.wrap.of 'abcxx' stripEnd 'x'
assert value == 'abc'
leftPad¶
Pads, the specified pad string, to the left of the string length amount of times.
Syntax
StringWrapper wrap.leftPad(int length).with(String pad)
StringWrapper wrap.leftPad(int length).get()
# Similar as pad equals ' '
Example
final String value = p6.wrap.of 'example' leftPad 10 with ' '
assert value == ' example'
final String value = p6.wrap.of 'example' leftPad 10 with '.'
assert value == '...example'
final String value = p6.wrap.of 'example' leftPad 10 get()
assert value == ' example'
rightPad¶
Pads, the specified pad string, to the right of the string length amount of times.
Syntax
StringWrapper wrap.rightPad(int length).with(String pad)
StringWrapper wrap.rightPad(int length).get()
# Similar as pad equals ' '
Example
final String value = p6.wrap.of 'example' rightPad 10 with ' '
assert value == 'example '
final String value = p6.wrap.of 'example' rightPad 10 with '.'
assert value == 'example...'
final String value = p6.wrap.of 'example' rightPad 10 get()
assert value == 'example '
Substring¶
left¶
Gets the leftmost length characters of the string.
Syntax
StringWrapper wrap.left(int length)
Example
final String value = p6.wrap.of 'example' left 3
assert value == 'exa'
right¶
Gets the rightmost length characters of the string.
Syntax
StringWrapper wrap.right(int length)
Example
final String value = p6.wrap.of 'example' right 3
assert value == 'ple'
substring¶
New Feature
Since 6.10.24
Extracts a substring, clamped to the string bounds.
Syntax
StringWrapper wrap.substring(int start)
StringWrapper wrap.substring(int start, int end)
Example
final value = p6.wrap.of 'hello' substring(1, 3)
assert value == 'el'
abbreviate¶
Abbreviates the string to a maximum width of maxWidth.
Syntax
StringWrapper wrap.abbreviate(int maxWidth).with(String ellipsis)
StringWrapper wrap.abbreviate(int maxWidth).get()
# Similar as ellipsis equals '...'
Example
final String value = p6.wrap.of 'example' abbreviate 5 with '...'
assert value == 'ex...'
final String value = p6.wrap.of 'example' abbreviate 5 with '.'
assert value == 'exam.'
final String value = p6.wrap.of 'example' abbreviate 5 get()
assert value == 'ex...'
truncate¶
New Feature
Since 6.10.24
Hard-cuts the string to maxLength characters (no ellipsis; see abbreviate).
Syntax
StringWrapper wrap.truncate(int maxLength)
Example
assert p6.wrap.of('hello world').truncate(5) == 'hello'
Editing¶
append¶
Appends the specified value to the string.
Syntax
StringWrapper wrap.append(String value)
Example
final String value = p6.wrap.of 'example' append '123'
assert value == 'example123'
prepend¶
Prepends the specified value to the string.
Syntax
StringWrapper wrap.prepend(String value)
Example
final String value = p6.wrap.of 'example' prepend '123'
assert value == '123example'
replace¶
Replaces all occurrences of target with replacement in the string.
Syntax
StringWrapper wrap.replace(String target).with(String replacement)
Example
final String value = p6.wrap.of 'example' replace 'e' with 'a'
assert value == 'axampla'
replaceRe¶
Replaces all occurrences of the regular expression target with replacement in the string.
Syntax
StringWrapper wrap.replaceRe(String regex).with(String replacement)
Example
final String value = p6.wrap.of 'example' replaceRe 'e.' with 'a'
assert value == 'aample'
replaceFirst¶
New Feature
Since 6.10.24
Replaces the first regex match (companion to replace and replaceRe).
Syntax
StringWrapper wrap.replaceFirst(String regex) with(String replacement)
Example
final value = p6.wrap.of 'a-a-a' replaceFirst('a') with('b')
assert value == 'b-a-a'
remove¶
New Feature
Since 6.10.24
Removes all occurrences of the specified sequence.
Syntax
StringWrapper wrap.remove(String seq)
Example
final value = p6.wrap.of 'a.b.c' remove '.'
assert value == 'abc'
reverse¶
New Feature
Since 6.10.24
Reverses the string.
Syntax
StringWrapper wrap.reverse()
Example
final value = p6.wrap.of 'abc' reverse()
assert value == 'cba'
repeat¶
New Feature
Since 6.10.24
Repeats the string the given number of times.
Syntax
StringWrapper wrap.repeat(int times)
Example
final value = p6.wrap.of 'ab' repeat 3
assert value == 'ababab'
defaultIfBlank¶
New Feature
Since 6.10.24
Returns the fallback when the string is blank.
Syntax
StringWrapper wrap.defaultIfBlank(String fallback)
Example
final value = p6.wrap.of '' defaultIfBlank 'n/a'
assert value == 'n/a'
escape¶
Escape the string.
Syntax
StringWrapper wrap.escape()
Example
final w = p6.wrap.of 'example' escape()
unescape¶
New Feature
Since 6.10.24
Reverses escape() (unescapes Java string literals).
Syntax
StringWrapper wrap.unescape()
Example
final value = p6.wrap.of 'a\\tb' unescape()
escapeXml / unescapeXml¶
New Feature
Since 6.10.24
Escapes/unescapes for XML 1.0 — use when hand-building XML fragments.
Syntax
StringWrapper wrap.escapeXml()
StringWrapper wrap.unescapeXml()
Example
final value = p6.wrap.of 'a<b>&"' escapeXml()
assert value == 'a<b>&"'
Tests & search¶
isBlank¶
Checks if the string is blank.
Syntax
boolean wrap.isBlank()
Example
final value = p6.wrap.of ' ' isBlank()
assert value == true
isNotBlank¶
Checks if the string is not blank.
Syntax
boolean wrap.isNotBlank()
Example
final value = p6.wrap.of ' ' isNotBlank()
assert value == false
isEmpty¶
Checks if the string is empty.
Syntax
boolean wrap.isEmpty()
Example
final value = p6.wrap.of '' isEmpty()
assert value == true
isNotEmpty¶
Checks if the string is not empty.
Syntax
boolean wrap.isNotEmpty()
Example
final value = p6.wrap.of '' isNotEmpty()
assert value == false
isNumeric¶
Checks if the string is numeric.
Syntax
boolean wrap.isNumeric()
Example
final value = p6.wrap.of '123' isNumeric()
assert value == true
final value = p6.wrap.of 'abc' isNumeric()
assert value == false
contains¶
Checks if the string contains the specified sequence seq.
Syntax
boolean wrap.contains(String seq)
Example
final value = p6.wrap.of 'example' contains 'amp'
assert value == true
final value = p6.wrap.of 'example' contains 'fee'
assert value == false
containsIgnoreCase¶
New Feature
Since 6.10.24
Case-insensitive contains.
Syntax
boolean wrap.containsIgnoreCase(String seq)
Example
assert p6.wrap.of('Hello').containsIgnoreCase('ELL')
startsWith¶
Checks if the string starts with the specified sequence seq.
Syntax
boolean wrap.startsWith(String seq)
Example
final value = p6.wrap.of 'example' startsWith 'ex'
assert value == true
final value = p6.wrap.of 'example' startsWith 'le'
assert value == false
endsWith¶
Checks if the string ends with the specified sequence seq.
Syntax
boolean wrap.endsWith(String seq)
Example
final value = p6.wrap.of 'example' endsWith 'le'
assert value == true
final value = p6.wrap.of 'example' endsWith 'ex'
assert value == false
equalsIgnoreCase¶
New Feature
Since 6.10.24
Case-insensitive equality.
Syntax
boolean wrap.equalsIgnoreCase(String seq)
Example
assert p6.wrap.of('ABC').equalsIgnoreCase('abc')
matches¶
New Feature
Since 6.10.24
Tests the whole string against a regex.
Syntax
boolean wrap.matches(String regex)
Example
assert p6.wrap.of('12345').matches('\\d+')
extract¶
New Feature
Since 6.10.24
Returns the first match, or the first capturing group when the pattern defines one, or an empty string when nothing matches.
Syntax
StringWrapper wrap.extract(String regex)
Example
final value = p6.wrap.of 'order-42' extract('\\d+')
assert value == '42'
indexOf¶
New Feature
Since 6.10.24
Returns the index of the first occurrence of the sequence, or -1 when absent.
Syntax
NumberWrapper wrap.indexOf(String seq)
Example
final value = p6.wrap.of 'hello' indexOf 'l' get()
assert value == 2
countMatches¶
Counts how many times the specified sequence seq is contained.
Syntax
NumberWrapper wrap.countMatches(String seq)
Example
final value = p6.wrap.of 'example' countMatches 'e' get()
assert value == 2
length¶
New Feature
Since 6.10.24
Returns the length of the string.
Syntax
NumberWrapper wrap.length()
Example
final value = p6.wrap.of 'hello' length() get()
assert value == 5
Split¶
split¶
Splits the string into an array of StringWrapper using the specified separator.
Syntax
ArrayWrapper wrap.split(String seq)
Example
final value = p6.wrap.of 'fee,foo,bar' split ',' size() get()
assert value == 3
lines / words¶
New Feature
Since 6.10.24
Splits into lines (on \r?\n) or whitespace-separated words.
Syntax
ArrayWrapper wrap.lines()
ArrayWrapper wrap.words()
Example
assert p6.wrap.of('a\nb\nc').lines().size().get() == 3
assert p6.wrap.of('the quick brown').words().size().get() == 3
Encoding & hashing¶
New Feature
Since 6.10.24
base64Encode / base64Decode / base64DecodeBytes¶
Syntax
StringWrapper wrap.base64Encode()
StringWrapper wrap.base64Decode() // UTF-8 String — for text
byte[] wrap.base64DecodeBytes() // raw bytes — for binary payloads
Warning
Use base64DecodeBytes() for binary data (images, PDFs, …). base64Decode() returns a
UTF-8 String and will corrupt non-text bytes.
Example
final value = p6.wrap.of 'hello' base64Encode()
assert value == 'aGVsbG8='
final bytes = p6.wrap.of(attachmentBase64).base64DecodeBytes()
p6.file.write bytes to targetUri
urlEncode / urlDecode¶
Syntax
StringWrapper wrap.urlEncode()
StringWrapper wrap.urlDecode()
Example
final value = p6.wrap.of 'a b&c' urlEncode()
assert value == 'a+b%26c'
md5 / sha256¶
Syntax
StringWrapper wrap.md5()
StringWrapper wrap.sha256()
Example
final value = p6.wrap.of 'abc' md5()
assert value == '900150983cd24fb0d6963f7d28e17f72'
bytes / asStream¶
New Feature
Since 6.10.24
Returns the string as UTF-8 bytes or a UTF-8 InputStream (handy for feeding file writes,
parsers, or HTTP bodies without dropping to getBytes(StandardCharsets.UTF_8)).
Syntax
byte[] wrap.bytes()
InputStream wrap.asStream()
Example
p6.file.write p6.wrap.of(payload).bytes() to targetUri
Combined example¶
Normalise a label into a slug, derive a short stable id, and round-trip through Base64.
final slug = p6.wrap.of(' ACME Corp / Invoice #42 ').trim().slugify()
assert slug == 'acme-corp-invoice-42'
final id = p6.wrap.of('user@example.com').lower().sha256().left(12)
final token = p6.wrap.of('hello world').base64Encode()
assert p6.wrap.of(token.toString()).base64Decode() == 'hello world'