Finding spaces, newlines and tabs with charAt ()

I am trying to check if there is a space, new line or tab at the current location of the character. Spaces work, but tabs and newlines are not used. Go figure, I use escapes for those, and just regular space for spaces ... What is the right way to find them in a specific place?

if(String.valueOf(txt.charAt(strt)).equals(" ") || txt.charAt(strt) == '\r' || txt.charAt(strt) == '\n' || txt.charAt(strt) == '\t') { //do stuff } 
+6
source share
4 answers

This works for me:

  char c = txt.charAt(strt); if (c == ' ' || c == '\t' || c == '\n' || c == '\r') System.out.println("Found one at " + strt); 

You work too, although it's a little harder. Why this does not work for you, I don’t know - maybe the line is badly formed? Are you sure you actually have tabs and stuff?

+8
source

It should work fine, check the input line. In addition, the space can be checked by comparing the space. Creating a new String object just for comparison is expensive.

+1
source

Looking at the docs for Editable in android, it returns a char . Therefore...

 if (txt.charAt(strt) == ' ' || txt.charAt(strt) == '\r' || txt.charAt(strt) == '\n' || txt.charAt(strt) == '\t') { //do stuff } 

Call the expected result.

0
source

This regular expression [\ s] will do all the work. This corresponds to a space equivalent to [\ t \ n \ r \ f].

0
source

Source: https://habr.com/ru/post/899574/


All Articles