• Register
1 vote
25 views

Problem:

Hello Kodlogs,

How can I iterate through characters in a string java? The question coming from a novice in java. So, please keep your answer as easier as possible.

Note: I can declare a string type in Java.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

Well, in Java, there are a couple of ways to iterate through characters in a string. As per your question, you can use the .charAt() or the .toCharArray() function. These two are the easiest and efficient ways to iterate through chars in a string. Let me give an example by using them in my program.

 Using .charAt() function:

public class MyClass{	
	public static void main (String[] args){
		String str = "I am Gavin";
		for (int n = 0; n < str.length(); n++){
			System.out.print(str.charAt(n));
		}
	}
}

Using .toCharArray() function:

public class MyClass{
	public static void main (String[] args){
		String str = "I am Gavin";
		char[] chars = str.toCharArray();
		for (char ch : chars){
			System.out.print(ch);
		}
	}
}

Above both program will produce the same output

I am Gavin

 

13 9 6
94,240 points

Related questions

0 votes
1 answer 4 views
4 views
Problem:
asked 4 days ago Ali 4.4k points
0 votes
1 answer 3 views
3 views
Problem: StringTokenizer? Convert the String to a char[] and iterate over that? Something else? tring s = "...stuff..."; for (int i = 0; i < s.length(); i++){ char c = s.charAt(i); //Process char } That's what I would do. It seems the diificult to me.
asked Feb 20 Ali 4.4k points
1 vote
1 answer 30 views
30 views
Problem: Hello all, I am struggling to solve a problem for the last thirty minutes using the c++ programing language. I was thinking of writing a program that will read the defined string and iterate through it and print all the words separately. Suppose I initialize a string with ... it very much How do you it or suggest me an easy way to do that. I look forward. Thanks for your kind assistance.
asked Jun 15, 2020 adamSw 11.3k points
0 votes
1 answer 10 views
10 views
Does anyone have an idea?
asked Dec 19, 2020 TeamScript 13.5k points
1 vote
1 answer 14 views
14 views
Problem: Here comes a newbie in Python programming. Recently one of my friends advised me to learn computer programming and suggested to start with Python. I have picked up the language as she told me it is easy for a beginner. Therefore, I joined an online course and started learning ... list is: a_list = [1, 3, 5, 7, 9] I am eagerly waiting for your answers. Thanks for your concern, guys.
asked Jun 30, 2020 adamSw 11.3k points
0 votes
1 answer 6 views
6 views
Problem: I want to iterate through each element in the map<string, int> without knowing any of its string-int values or keys. What I have so far: void output(map<string, int> table) { map<string, int>::iterator it; for (it = table.begin(); it != table.end(); it++) { //How do I access each element? } }
asked Dec 24, 2020 Mashhoodch 9.1k points
0 votes
1 answer 14 views
14 views
Problem: Hello Kodlogs! I am a beginner and started learning programming through an online course. In course, I am given an assignment in which I have to write a program that will count the occurrence of each character in a string and then print the number of ... with detailed comments so that I can understand the logic and program? The given string is: &ldquo;This is an assignment&rdquo;. Thanks!
asked Jan 6 Code Learner 5.9k points
0 votes
1 answer 21 views
21 views
Problem: Hello! I am a beginner, and my teacher gave me an assignment in which I have to write a program that takes a string as input from the user. If the string contains any consecutive duplicate characters then my program should remove all the consecutive occurrences of that ... . I am weak in programming and have almost no idea how I can do that. Can anybody here help me to solve this problem?
asked Jan 2 Code Learner 5.9k points
0 votes
1 answer 21 views
21 views
I need to find frequency of charts in a string? can anyone here help me?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 2 views
2 views
Problem: What I'd like to know is if the cost of repeatedly calling the charAt method during a long iteration ends up being either less than or greater than the cost of performing a single call to toCharArray at the beginning and then directly accessing the array ... in mind JIT warm-up time, JVM start-up time, etc. and not just the difference between two calls to System.currentTimeMillis().
asked 2 days ago Ali 4.4k points