Jump to content

Strip whitespace from a string/Top and tail: Difference between revisions

(K)
Line 1,153:
=={{header|Java}}==
Java has the ''trim'' and ''strip'' operations. Both will achieve a similar result.<br />
The ''<code>trim''</code> method works on Unicode values under <kbd>0x20</kbd>. Whereas, ''strip'' will remove all Unicode white-space characters.
<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...[] characters) {
int index = 0;
for (char characterA : string.toCharArray()) {
Line 1,181:
</syntaxhighlight>
<syntaxhighlight lang="java">
String removeTrailing(String string, char...[] characters) {
for (int index = string.length() - 1; index >= 0; index--) {
for (char character : characters) {
118

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.