Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add trimToNull method on GrailsStringUtils #13807

Open
wants to merge 1 commit into
base: 7.0.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion grails-core/src/main/groovy/grails/util/GrailsStringUtils.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -191,4 +191,28 @@ abstract class GrailsStringUtils extends StringUtils{
stripFilenameExtension( getFilename(path) )
}

/**
* Removes all space from both ends of this String returning
* {@code null} if the String is empty ("") after the trim
* or if it is {@code null}.
*
* <p>The String is trimmed using {@link String#trim()}.
*
* <pre>
* GrailsStringUtils.trimToNull(null) = null
* GrailsStringUtils.trimToNull("") = null
* GrailsStringUtils.trimToNull(" ") = null
* GrailsStringUtils.trimToNull("xyz") = "xyz"
* GrailsStringUtils.trimToNull(" xyz ") = "xyz"
* </pre>
*
* @param str the String to be trimmed, may be null
* @return the trimmed String,
* {@code null} if only containing space, empty or null String input
* @since 7.0.0
*/
static String trimToNull(String str) {
String trimmed = str?.trim()
return hasLength(trimmed) ? trimmed : null
}
}
Loading