Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: A little confused about charAt()

  1. #1
    beijct is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default 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?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default 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:

    Java Code:
    String str = "The quick brown fox jumps over the lazy dog.";
    
    char c = str.charAt(10);
    System.out.println("c = " + c);
    This code will give you the letter "b" which is the character at index number 10. String index start from 0.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: A little confused about charAt()

    Quote Originally Posted by beijct View Post
    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?
    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,

    Jos
    wsaryada likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. charAt problem
    By tangel in forum New To Java
    Replies: 4
    Last Post: 10-26-2011, 04:49 PM
  2. Str.charAt() ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 21
    Last Post: 08-23-2011, 07:42 AM
  3. Why won't charAt() work?
    By MetalR0 in forum New To Java
    Replies: 6
    Last Post: 08-04-2011, 09:46 AM
  4. Space for charAt()?
    By Tussmann in forum New To Java
    Replies: 5
    Last Post: 11-02-2010, 06:57 PM
  5. Help with charAt()
    By HackerOfDoom in forum New To Java
    Replies: 7
    Last Post: 03-21-2010, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •