Sometimes I had to remove leading and trailing whitespace for each feature, so I tried to find out how to delete each language.
Python
By specifying an arbitrary character string in the argument, the character string can be deleted from the beginning and end of the character string. If you omit the argument, you can remove spaces and tabs at the beginning and end of the string. Spaces between characters are not removed.
How to use There is a half-width space at the beginning and a tab at the end.
str = ' Hello World '
print(str.strip())
result
Hello World
If you want to remove the beginning Use the lstrip method. I put a tab at the beginning.
str = ' Hello World '
print(str.lstrip())
result
Hello World
If you want to remove the end Use the rstrip method. There is a double-byte space at the end.
str = ' Hello World '
print(str.lstrip())
result
Hello World
JavaScript
You can remove spaces and tabs at the beginning and end of the string. Spaces between characters are not removed ~~ It is not possible to delete a blank on only one side. ~~ ※it is complete. Thank you for your advice. If you want to remove whitespace on only one side, you can use trimStart () and trimEnd (). Since I tried it in Chrome this time, I am using the aliases trimMeft () and trimRight ().
How to use
let str = ' Hello World ';
console.log(str);
console.log(str.trim());
result
Hello World
Hello World
Full-width characters are also deleted.
let str = ' Hello World ';
console.log(str.trim());
result
Hello World
Use the trimLeft method to remove leading whitespace and the trimRight method to remove trailing whitespace.
According to MDN web docs
trimLeft method
For consistency with functions like String.prototype.padStart, the standard method name is set to trimStart.
However, for web compatibility reasons, trimLeft is left as an alias for trimStart. Some engines interpret it as: String.prototype.trimLeft.name === "trimStart";
trimRight method
For consistency with functions like String.prototype.padEnd, the standard method name is set to trimEnd.
However, for web compatibility reasons, trimRight is left as an alias for trimEnd. Some engines interpret it as: String.prototype.trimRight.name === "trimEnd";
Because it is said that Depending on the browser, there are support and non-support, You may also use an alias.
let str = ' Hello World ';
console.log(str.trimLeft());
console.log(str.trimRight());
result
Hello World
Hello World
Java Use the trim method.
How to use
String str = " Hello World ";
System.out.println(str);
System.out.println(str.trim());
result
Hello World
Hello World
You cannot remove double-byte spaces with the Java trim method.
String str = " Hello World ";
System.out.println(str);
System.out.println(str.trim());
result
Hello World
To remove double-byte spaces, Use the replaceAll method I will replace it with a regular expression.
The following is the case when there is a double-byte space at the end.
String str = "Hello World ";
System.out.println(str.replaceAll("[ ]+$", ""));
result
Hello World
https://engineer-club.jp/java-trim
https://uxmilk.jp/12804
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/trim
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/TrimLeft
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/TrimRight
Recommended Posts