Skip to content

Commit 80792f8

Browse files
authored
Merge pull request #13807 from grails/add-trimToNull-GrailsStringUtils
add trimToNull method on GrailsStringUtils
2 parents ff5cc88 + 931befc commit 80792f8

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

grails-core/src/main/groovy/grails/util/GrailsStringUtils.groovy

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -191,4 +191,30 @@ abstract class GrailsStringUtils extends StringUtils{
191191
stripFilenameExtension( getFilename(path) )
192192
}
193193

194+
/**
195+
* Removes all space from both ends of this String returning
196+
* {@code null} if the String is empty ("") after the trim
197+
* or if it is {@code null}.
198+
*
199+
* <p>The String is trimmed using {@link String#trim()}.
200+
*
201+
* <pre>
202+
* GrailsStringUtils.trimToNull(null) = null
203+
* GrailsStringUtils.trimToNull("") = null
204+
* GrailsStringUtils.trimToNull(" ") = null
205+
* GrailsStringUtils.trimToNull("xyz") = "xyz"
206+
* GrailsStringUtils.trimToNull(" xyz ") = "xyz"
207+
* </pre>
208+
*
209+
* based on https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/StringUtils.java#L8838
210+
*
211+
* @param str the String to be trimmed, may be null
212+
* @return the trimmed String,
213+
* {@code null} if only containing space, empty or null String input
214+
* @since 7.0.0
215+
*/
216+
static String trimToNull(String str) {
217+
String trimmed = str?.trim()
218+
return hasLength(trimmed) ? trimmed : null
219+
}
194220
}

grails-core/src/test/groovy/grails/util/GrailsStringUtilsSpec.groovy

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,26 @@ import spock.lang.Specification
2323
*/
2424
class GrailsStringUtilsSpec extends Specification{
2525

26+
static final String FOO = 'foo'
27+
static final String TRIMMABLE
28+
static final String NON_TRIMMABLE
29+
30+
static {
31+
def trimmable = new StringBuilder()
32+
def nonTrimmable = new StringBuilder()
33+
(0..<Character.MAX_VALUE).each { i ->
34+
char ch = (char) i
35+
if (Character.isWhitespace(ch) && i > 32) {
36+
nonTrimmable.append(ch)
37+
}
38+
}
39+
(0..32).each { int i ->
40+
trimmable.append((char) i)
41+
}
42+
TRIMMABLE = trimmable.toString()
43+
NON_TRIMMABLE = nonTrimmable.toString()
44+
}
45+
2646
void "Test toBoolean"() {
2747
expect:
2848
GrailsStringUtils.toBoolean("on") == true
@@ -63,4 +83,17 @@ class GrailsStringUtilsSpec extends Specification{
6383
GrailsStringUtils.trimStart("abc", "ab") == 'c'
6484
GrailsStringUtils.trimStart("abc", "c") == 'abc'
6585
}
86+
87+
void "Test trimToNull method"() {
88+
expect:
89+
GrailsStringUtils.trimToNull(FOO + " ") == FOO
90+
GrailsStringUtils.trimToNull(" " + FOO + " ") == FOO
91+
GrailsStringUtils.trimToNull(" " + FOO) == FOO
92+
GrailsStringUtils.trimToNull(FOO + "") == FOO
93+
GrailsStringUtils.trimToNull(" \t\r\n\b ") == null
94+
GrailsStringUtils.trimToNull(TRIMMABLE) == null
95+
GrailsStringUtils.trimToNull(NON_TRIMMABLE) == NON_TRIMMABLE
96+
GrailsStringUtils.trimToNull("") == null
97+
GrailsStringUtils.trimToNull(null) == null
98+
}
6699
}

0 commit comments

Comments
 (0)