Concept
How do you find the length of a string and iterate over its characters?
ComputerScienceOne / String Methods
"When accessing individual characters in a string, it is necessary to know the length of the string so that we do not access invalid characters. The length() method returns an int that represents the number of characters in the string. String s = \"Hello World!\"; char last = s.charAt(s.length()-1); //last is '!' Using this method, we can easily iterate over each character in a string. for(int i=0; i<fullName.length(); i++) { System.out.printf(\"fullName[%d] = %c\n\n\", i, fullName.charAt(i)); } We can also use toCharArray and an enhanced for loop: for(char c : fullName.toCharArray()) { System.out.println(c); }"
Related Ideas
- How can you determine the length of a PHP string?ComputerScienceOne · String Functions
- How can strlen determine the length of a C string?ComputerScienceOne · String Library
- How can you compute a substring of a string?ComputerScienceOne · Arrays of Strings
- How can individual characters in a string be accessed safely?ComputerScienceOne · Basic Operations
- How can strings be created and assigned to variables?ComputerScienceOne · Basic Operations