Strip whitespace from a string/Top and tail: Difference between revisions
Strip whitespace from a string/Top and tail (view source)
Revision as of 05:05, 17 May 2023
, 1 year ago→{{header|Java}}
(K) |
|||
Line 1,153:
=={{header|Java}}==
Java has the ''trim'' and ''strip'' operations. Both will achieve a similar result.<br />
The
<syntaxhighlight lang="java">
" abc".stripLeading()
Line 1,168:
For more control over what characters should be removed, you could implement your own methods.
<syntaxhighlight lang="java">
String removeLeading(String string, char
int index = 0;
for (char characterA : string.toCharArray()) {
Line 1,181:
</syntaxhighlight>
<syntaxhighlight lang="java">
String removeTrailing(String string, char
for (int index = string.length() - 1; index >= 0; index--) {
for (char character : characters) {
|