What is charAt (0) used for?

1

I have an example code that says the following:

(uno.getText().trim().charAt(0)+"\n"+   
dos.getText().trim().charAt(0)+"\n"+   
tres.getText().trim().charAt(0)+"\n"+   
cuatro.getText().trim().charAt(0));   

We did this to show in textarea the initials of the names that the user entered, but I do not remember how it worked.

I would also need to know what the "\n"+ does, and why trim() is used

    
asked by Melannie Nichole 06.08.2017 в 20:49
source

1 answer

4
  • charAt (index) is a String function that returns the character located in the index position of the string

  • / n is the special character for string jump

  • trim is also a function of String, but in this case it returns the same string after having removed the blanks at the beginning and end of the string

In the code that you published first the spaces with trim are eliminated, trim returns another String so you can apply any function of String again, in this case charAt (0) returns the initial, which is what you are looking for .

Finally, to format the results, they are concatenated separated by a line break

Sources: oracle docs

    
answered by 06.08.2017 в 21:06