The easiest way to for-each every char
in a String
is to use toCharArray()
:
for (char ch: "xyz".toCharArray()) { }
This gives you the conciseness of for-each construct, but unfortunately String
(which is immutable) must perform a defensive copy to generate the char[]
(which is mutable), so there is some cost penalty.
From the documentation:
[
toCharArray()
returns] a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
There are more verbose ways of iterating over characters in an array (regular for loop, CharacterIterator
, etc) but if you’re willing to pay the cost toCharArray()
for-each is the most concise.
Related Posts:
- How to convert a char to a String?
- How to convert/parse from String to char in java?
- Illegal Escape Character “\”
- Java compressing Strings
- Unclosed Character Literal error
- How do I convert a String to an int in Java?
- How to format strings in Java
- How does the Java ‘for each’ loop work?
- How does the Java ‘for each’ loop work?
- How to split a string in Java
- How do I compare strings in Java?
- Reverse a string in Java
- Reverse a string in Java
- Convert String to double in Java
- “Char cannot be dereferenced” error
- Java String new line
- How can I convert a char to int in Java? [duplicate]
- How to check if a String contains another String in a case insensitive manner in Java?
- Java split string to array [duplicate]
- String concatenation: concat() vs “+” operator
- How to remove the last character from a string?
- Convert int to char in java
- Java string to date conversion
- How to replace � in a string
- Change date format in a Java string
- What are all the escape characters?
- Converting String to “Character” array in Java
- Best way to convert an ArrayList to a string
- How to split a String by space
- Iterate through a HashMap [duplicate]
- How can I prevent java.lang.NumberFormatException: For input string: “N/A”?
- How to remove single character from a String
- Iterate through a HashMap [duplicate]
- How to parse this string in Java?
- How to split a string in Java
- What does regular expression \\s*,\\s* do?
- How to convert a Binary String to a base 10 integer in Java
- Left Hand Side of an Assignment must be a Variable CharAt
- How to capitalize the first letter of a String in Java?
- Java way to check if a string is palindrome
- Return string Input with parse.string
- Java – removing first character of a string
- Understanding the difference between null and ‘\u000’ in Java
- Explain the use of a bit vector for determining if all characters are unique
- Explain the use of a bit vector for determining if all characters are unique
- Sort a single String in Java
- How to remove single character from a String
- What is Java String interning?
- What is Java String interning?
- What is Java String interning?
- Why is there no String.Empty in Java?
- What condition does while(true) test? When is it true and false?
- Using Enum values as String literals
- What is the easiest/best/most correct way to iterate through the characters of a string in Java?
- What does the colon (:) operator do?
- int cannot be converted to string?
- Convert String to int array in java
- How to sort an array of objects in Java?
- How to sort an array of objects in Java?
- How to compress a String in Java?
- java.lang.NumberFormatException: empty String
- How to check if a String is numeric in Java
- Bank Account Java Program
- Converting A String To Hexadecimal In Java
- How would I use a while loop to keep requesting user input
- Simple way to repeat a string
- Java end of file
- Add String Array to ArrayList
- StringIndexOutOfBoundsException String index out of range: 0
- difference between equals() and hashCode()
- How can I check if a single character appears in a string?
- Difference between String replace() and replaceAll()
- Java String to SHA1
- Replace a character at a specific index in a string?
- Determine if a String is an Integer in Java [duplicate]
- How to convert a String to CharSequence?
- How do I convert a String to a BigInteger?
- Create a two dimensional string array anArray[2][2]
- How to use a Do-while loop that continuously prompts a user?
- How to convert a String into an ArrayList?
- How do I break out of nested loops in Java?
- java: use StringBuilder to insert at the beginning
- How do I get the last character of a string?
- Cannot invoke toString() on the primitive type int
- How to compare character ignoring case in primitive types
- Java Not Greater than Or Equal to Operator for Char Type
- Append a single character to a string or char array in java?
- convert string to arraylist
in java - How to insert multiple tabs string in java?
- Append a single character to a string or char array in java?
- String is immutable. What exactly is the meaning?
- Why cannot cast Integer to String in java?
- Removing double quotes from a string in Java
- Occurrences of substring in a string
- Append a single character to a string or char array in java?
- for each loop in groovy
- How to make a Java Main Menu Loop after using a case
- Quotation marks inside a string
- In Java, how do I parse XML as a String instead of a file?
- Fastest way to put contents of Set
to a single String with words separated by a whitespace?