Results 1 to 3 of 3
Thread: A little confused about charAt()
- 11-20-2012, 02:56 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
A little confused about charAt()
Hi all, I'm trying to use the charAt() method to, obviously, reference a character by its index in a string; however, I'm a little confused about the paramaters it takes. Docs.oracle says the only parameter it takes is an index but that seems wrong, which string exactly am I looking at when using charAt() and how would I specify which string I want it to look at?
- 11-20-2012, 04:15 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: A little confused about charAt()
Why do you think the documentation is wrong? The charAt() method of the java.lang.String class is only need a single method which is the index of the character in the string that you want to read. And the parameter type is an integer. Here is an example how to use it:
This code will give you the letter "b" which is the character at index number 10. String index start from 0.Java Code:String str = "The quick brown fox jumps over the lazy dog."; char c = str.charAt(10); System.out.println("c = " + c);Website: Learn Java by Examples
- 11-20-2012, 09:36 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Re: A little confused about charAt()
I can understand your confusion; from a pure procedural point of view a charAt( ... ) method would need two parameters: a String and and int index; in an object oriented world methods have one implicit parameter: the object on which you call the method; in old versions of C++ there always was a first explicit parameter named 'this'; it was a pointer to this additional parameter. We don't need to program like that anymore, i.e. the compiler takes care of it all; e.g. when we write "foo".charAt(0) the compiler translates it for us to charAt("foo", 0) (and there's your second parameter again)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
charAt problem
By tangel in forum New To JavaReplies: 4Last Post: 10-26-2011, 04:49 PM -
Str.charAt() ?!
By HearT.Hunt3r in forum New To JavaReplies: 21Last Post: 08-23-2011, 07:42 AM -
Why won't charAt() work?
By MetalR0 in forum New To JavaReplies: 6Last Post: 08-04-2011, 09:46 AM -
Space for charAt()?
By Tussmann in forum New To JavaReplies: 5Last Post: 11-02-2010, 06:57 PM -
Help with charAt()
By HackerOfDoom in forum New To JavaReplies: 7Last Post: 03-21-2010, 05:27 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks