The method you're looking for is charAt. Here's an example:

String text = "foo";
char charAtZero = text.charAt(0);
System.out.println(charAtZero); // Prints f

For more information, see the Java documentation on String.charAt. If you want another simple tutorial, this one or this one.

If you don't want the result as a char data type, but rather as a string, you would use the Character.toString method:

String text = "foo";
String letter = Character.toString(text.charAt(0));
System.out.println(letter); // Prints f

If you want more information on the Character class and the toString method, I pulled my info from the documentation on Character.toString.

Answer from Ricardo Altamirano on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_indexof.asp
Java String indexOf() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... String myStr = "Hello planet earth, you are a great planet."; System.out.println(myStr.indexOf("planet"));
Discussions

In Java How Do You Assign A Char To A Particular Index In A String?
You can't manipulate a string like that. Java Strings are immutable; in order to change them, you have to create a new one; easiest way would be using substring to cut out the parts of the string you want versus the one character you don't. Furthermore, charAt just gives you the character from the string; it's not a reference to the character than can be manipulated, it's just a copy of the character value. More on reddit.com
🌐 r/learnprogramming
3
2
November 13, 2020
Find all index of occurrences of character in a string
Why would you even need the indexOf method? The naive, straightforward approach is to use .charAt and to loop over the characters in the string using a forloop. (TBH, .indexOf does exactly the same under the hood, just adds a starting index and it stops on the first occurrence). Honestly, the only way to learn proper programming is to stop searching for solutions and to sit down and try to work out your own solutions, because otherwise, you get in over your head, as in your case ending with code that you might have been able to somewhat reproduce, but that you don't understand. Start with pen(cil) and paper and try to figure out how you, the person, would address a problem. Only once you have found a solution start thinking about programming it. And don't memorize. Especially not code. Code adapts to the situation and is only the end product, not the starting point. Edit: Very mature move/s of you blocking people that offer help and advice to you just because you feel personally attacked or disagree with what has been said. More on reddit.com
🌐 r/learnjava
10
3
October 7, 2024
Java, how to count the indexes of a String array readin by Scanner?
.length returns the number of characters The length attribute of an array tells you how many elements the array can hold. For the split method, the array’s length matches the number of Strings that were split-off from the original String. The maximum index of an array is 1 less than its length, due to 0-indexing. I need a way to handle/avoid the out of bounds exception which may occur if I access the 3rd index. Check whether the sArr.length > 3. More on reddit.com
🌐 r/learnprogramming
2
2
November 18, 2021
[JAVA] Best practice to obtain substring using index of a character that may not be in original string.
Step through the string, one character at a time, looking for a comma. If you reach either the end of the string or a comma, you know what the first id is. More on reddit.com
🌐 r/learnprogramming
7
1
April 10, 2015
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › manipstrings.html
Manipulating Characters in a String (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
The index of the first character is 0, while the index of the last character is length()-1. For example, the following code gets the character at index 9 in a string:
🌐
QBasic on Your Computer
chortle.ccsu.edu › javaLessons › chap45 › ch45_04.html
String Indexing
The beginning character of a string corresponds to index 0 and the last character corresponds to the index (length of string)-1.
🌐
CodingBat
codingbat.com › doc › java-string-indexof-parsing.html
Java String indexOf Parsing
"Parsing" is the process of taking a string from a file or the network or the user, and processing it to extract the information we want. A common strategy is to use a few calls to indexOf() to figure out the index numbers of something interesting inside the string.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-charat-method-example
Java String charAt() Method - GeeksforGeeks
December 23, 2024 - String charAt() method in Java returns the character at the specified index in a string. The Index of the first character in a string is 0, the second character is 1, and so on.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › getting a character by index from a string in java
Getting a Character by Index From a String in Java | Baeldung
November 24, 2025 - The index ranges from 0 (the first character) to the total length of the String – 1 (the last character).
🌐
LabEx
labex.io › tutorials › java-how-to-work-with-string-indexing-in-java-468026
How to work with string indexing in Java | LabEx
Master Java string indexing techniques with practical examples, learn character extraction, substring manipulation, and advanced string handling methods for efficient programming.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - Allocates a new String that contains characters from a subarray of the Unicode code point array argument. The offset argument is the index of the first code point of the subarray and the count argument specifies the length of the subarray.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-indexof
Java String indexOf() Method - GeeksforGeeks
November 19, 2024 - In the above example, it finds the first occurrence of the character g in the string "Welcome to geeksforgeeks" and prints its index i.e. 11. ... This method returns the index of the first occurrence of the specified character by starting the search at the specified index. ... // Java code to demonstrate the working of String indexOf(char ch, int strt) public class Index2 { public static void main(String args[]) { String s = new String("Welcome to geeksforgeeks"); System.out.print( "Found g after 13th index at position: "); System.out.println(s.indexOf('g', 13)); } }
🌐
Reddit
reddit.com › r/learnjava › find all index of occurrences of character in a string
r/learnjava on Reddit: Find all index of occurrences of character in a string
October 7, 2024 -

https://books.google.com.np/books?id=h0Uhz4lBoF8C&pg=PA172&dq=find+index+of++occurrences+character+in+a+string&hl=en&newbks=1&newbks_redir=0&sa=X&ved=2ahUKEwjI-qzhp_uIAxX5S2wGHUHiL_MQ6AF6BAgGEAI#v=onepage&q&f=false

I found this solution here and re-wrote my code for it. And it works. And I was also attempting to use similar logic. However, I don't really grasp it by heart till of now. I am not able to internalize it.

if (wordToGuess.contains(userGuess)) {
                int aIndex = -1;
                while ((aIndex = wordToGuess.indexOf(userGuess, ++aIndex)) > -1) {
                    indexArr[aIndex] = 1;
                }
                displayAsterik(indexArr, wordToGuess);
            } else {
                mistakes = mistakes + 1;
            }

This is the code, the while loop is the part I am failing to analyze. I can obviously trace this loop with each input and see it works. However, the bigger question is designing such solutions in which cases in the future.

Top answer
1 of 5
7
Why would you even need the indexOf method? The naive, straightforward approach is to use .charAt and to loop over the characters in the string using a forloop. (TBH, .indexOf does exactly the same under the hood, just adds a starting index and it stops on the first occurrence). Honestly, the only way to learn proper programming is to stop searching for solutions and to sit down and try to work out your own solutions, because otherwise, you get in over your head, as in your case ending with code that you might have been able to somewhat reproduce, but that you don't understand. Start with pen(cil) and paper and try to figure out how you, the person, would address a problem. Only once you have found a solution start thinking about programming it. And don't memorize. Especially not code. Code adapts to the situation and is only the end product, not the starting point. Edit: Very mature move/s of you blocking people that offer help and advice to you just because you feel personally attacked or disagree with what has been said.
2 of 5
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Quora
quora.com › How-do-you-use-indexOf-and-lastIndexOf-in-Java
How to use indexOf and lastIndexOf in Java - Quora
Answer (1 of 2): indexOf and ... imported by default in all java programs. indexOf returns only the index of first occurrence of the substring present in specified parent string elseif not present returns -1. There...
🌐
Shiksha
shiksha.com › home › it & software › colleges in india
All About indexOf() Method of String Class in Java
December 16, 2024 - Find 12302 best IT & Software Colleges in India. Compare Fees, IT & Software Courses, Admission Process, Accepted Exams, Cut off, Placements, and Student Reviews.
🌐
iO Flood
ioflood.com › blog › indexof-java
Java's String IndexOf Method: A Detailed Usage Guide
February 26, 2024 - The indexOf() method in Java is a part of the String class and is used to find the position of a character or substring in a string. The method returns the index within this string of the first occurrence of the specified character or substring.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › indexOf
Java String indexOf() - Find Character Index | Vultr Docs
December 17, 2024 - The indexOf() method in Java is a powerful tool used to find the position of a specific character or substring within a string. This function returns the index within the calling String object of the first occurrence of the specified value, ...
🌐
TutorialsPoint
tutorialspoint.com › search-index-of-a-character-in-a-string-in-java
Search index of a character in a string in Java
June 26, 2020 - public class Demo { public static void main(String[] args) { String myStr = "amit"; int strIndex = 0; System.out.println("String: "+myStr); // finding index of character t strIndex = myStr.indexOf('t'); System.out.println("Character m found at index: "+strIndex); } }
🌐
Udemy
blog.udemy.com › home › understanding the java string indexof method
Understanding the Java String IndexOf Method - Udemy Blog
December 4, 2019 - The Java string indexOf method is a function that is used to get the integer value of a particular index of String type object, based on a criteria specified in the parameters of the IndexOf method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-get-a-character-from-a-string
Java Program to Get a Character from a String - GeeksforGeeks
July 29, 2024 - Java Collection · Last Updated : 29 Jul, 2024 · Given a String str, the task is to get a specific character from that String at a specific index. Examples: Input: str = "Geeks", index = 2 Output: e Input: str = "GeeksForGeeks", index = 5 Output: ...