Estoy usando expresiones regulares para contar la cantidad de caracteres de un texto, sin contar espacios.
El problema es que siempre cuenta un caracter de más (es decir, si el texto tiene 476 caracteres, retorna 477, si son 680, retorna 681).
Lo que tengo para contar los caracteres es esto:
int characters = text.split("\\S").length;
"text" es un string en donde guardé lo que leí desde el .txt. Utilicé la misma manera para contar oraciones y palabras y no hubo problemas.
Una manera en la que el resultado me dió bien fue haciendo:
int characters = text.replaceAll(" ", "").split("").length;
Pero siento que no tendría que ser necesario el replaceAll()
También podría simplemente restarle 1, pero quiero saber por qué pasa esto.
Por ejemplo, este fragmento de texto: Retorna que tiene 477 caracteres, pero al probar en regex101, hay 476 matches.
This is the page of the Simple English Wikipedia. A place where people work together to write encyclopedias in different languages. That includes children and adults who are learning English. There are 142,262 articles on the Simple English Wikipedia. All of the pages are free to use. They have all been published under both the Creative Commons License 3 and the GNU Free Documentation License. You can help here! You may change these pages and make new pages. Read the help pages and other good pages to learn how to write pages here. You may ask questions at Simple talk.