Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

how do you use .length() and not count the spaces from as part of your character length

+3 votes
asked Oct 10 in CSC 211 (F17) by Thomas Ayele (2,935 points)

2 Answers

0 votes
I would suggest declaring a new variable to count only characters without spaces in that string. You have to use a for loop and a method charAt() to check (from the first to the last character) whether it equals to ' ' or not. If not, increase that new variable.
answered Oct 10 by Tiffany Nguyen (2,268 points)
0 votes

Or you can try this code below. It technically will remove the spaces and count the length of that string later:

System.out.println(test.replace(" ", "").length()); < where test is a String variable >

answered Oct 10 by Tiffany Nguyen (2,268 points)
...