To determine if a String string has spaces in Java

Asked 1 years ago, Updated 1 years ago, 71 views

Please tell me how to check if there is a space in the string in Java. I'm getting a string, but if the user enters only spaces, I don't want to let them enter only spaces, but I think I can see that there's a space in the string, but I'm not sure what to do in this case. Does anyone know?

java string whitespace

2022-09-22 11:58

1 Answers

if (string.trim().length() > 0) Write trim(). trim() is a method that removes the spaces before and after the string, and if the string is made up of only spaces, 0 will appear when returning the string length after trim().


2022-09-22 11:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.