Results 1 to 4 of 4
- 08-20-2008, 06:05 PM #1
Member
- Join Date
- Aug 2008
- Location
- kolkata
- Posts
- 21
- Rep Power
- 0
how to get the characters one by one from a String?
I'm a newbie, I was learning the "String" & got stuck on a question i.e how to get the characters one by one from a String?
Actually I had a program the will take one sentence from the user. That will give a output-
The no of consonants, vowels, digits on that line.
I went on like this...
code:
import java.util.*;
class TestSentence{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter any Sentence...:");
String s1=s.nextLine();
//
...
}
now I've that String...but can't read one by one characters from that as we can do from the array by their index....
So, if you tell me how to do this I'll be grateful to you.........
- 08-20-2008, 06:16 PM #2
Java Code:public class Test { public static void main(String[] args) { String s = "hello world"; System.out.println("s = " + s); char[] chars = s.toCharArray(); System.out.printf("chars[%d] = %s%n", 3, chars[3]); String fourth = String.valueOf(chars[3]); String sixth = s.substring(6, 7); System.out.printf("fourth = %s sixth = %s%n", fourth, sixth); } }
- 08-20-2008, 06:33 PM #3
Also look at String.charAt() and StringBuffer class
- 08-20-2008, 08:56 PM #4
Member
- Join Date
- Aug 2008
- Location
- kolkata
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
characters + strings
By Gilgamesh in forum New To JavaReplies: 3Last Post: 03-02-2008, 09:10 PM -
How to split a string into multiple lines of x characters each
By JackJ in forum New To JavaReplies: 3Last Post: 12-17-2007, 02:35 AM -
Removing characters
By kDude in forum New To JavaReplies: 3Last Post: 12-03-2007, 02:38 AM -
special characters
By ravian in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:28 PM -
Getting all characters in a String
By Alayna in forum New To JavaReplies: 2Last Post: 05-20-2007, 11:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks