Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex to count & delete spaces at beginning of a line?

Tags:

java

regex

Searched other questions couldn't find any results.

I've written a regex to delete the spaces from the beginning of the line, but I need to count them and have them at the beginning of the line?

scan.nextLine().replaceAll("\\s+", "").trim();

Above is the regex (it's in a while loop). I'm reading in the text in a while loop to check if there is more text and it works fine but I don't know how I can print an integer with the number of spaces removed.

like image 265
user3093095 Avatar asked Nov 28 '22 15:11

user3093095


1 Answers

If you want to count the white spaces at the beginning of a string:

String s = "  123456";
int count = s.indexOf(s.trim());
like image 109
Mohammed Alokshiya Avatar answered Dec 05 '22 20:12

Mohammed Alokshiya