Remove leading and trailing whitespace in Python, JavaScript, or Java

Sometimes I had to remove leading and trailing whitespace for each feature, so I tried to find out how to delete each language.

Python

strip method

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

trim method

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

reference

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

Remove leading and trailing whitespace in Python, JavaScript, or Java
Express Python yield in JavaScript or Java
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Differences in syntax between Python and Java
How to measure processing time in Python or Java
Get in touch with functional programming in JavaScript or Python 3
I compared Java and Python!
I tried programming the chi-square test in Python and Java.
Stack and Queue in Python
Unittest and CI in Python
Python or and and operator trap
Graph the Poisson distribution and the Poisson cumulative distribution in Python and Java, respectively.
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
How to display bytes in the same way in Java and Python
Detect and process signals in Java.
Difference between java and python (memo)
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
[Python] return A [or / and] B
Compare Python and JavaScript array loops
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Let's throw away JavaScript and write a web front end in Python!
[Super basic] Compare Python, Java and JavaScript (variable, if statement, while statement, for statement)
Notes using cChardet and python3-chardet in Python 3.3.1.
Differences between Ruby and Python in scope
[Python] Use and and or when creating variables
AM modulation and demodulation in Python Part 2
Closure 4 language comparison (Python, JavaScript, Java, C ++)
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
(Java, JavaScript, Python) Comparison of string processing
Line graphs and scale lines in python
Implement FIR filters in Python and C
Check and receive Serial port in Python (Port check)
OR the List in Python (zip function)
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Referencing INI files in Python or Ruby
Dealing with "years and months" in Python
Scraping a website using JavaScript in Python
Read and write JSON files in Python
Easily graph data in shell and Python
Private methods and fields in python [encryption]
Find and check inverse matrix in Python