How to access particular character of a string without using pre defined functions?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puneetsardana88
    New Member
    • Aug 2009
    • 57

    How to access particular character of a string without using pre defined functions?

    I want to know how to access particular character of a String without using any pre defined functions(like charAt etc)? Since there are no pointers in java and the concept of character array being used as a String...So is there any way to do this?
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    No. If you can't use any of the methods of the String (or other) class then you can't determine what any character is.

    Then again without using some method or other, it's hard to see that you can do anything at all.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Strings are immutable so it won't let you fiddle with its internal data buffer at all, you have to use the methods it supplies for that purpose (such as charAt(...)). It doesn't supply any methods for changing that buffer at all, you can only read from the buffer.

      kind regards,

      Jos

      Comment

      • myusernotyours
        New Member
        • Nov 2007
        • 188

        #4
        And why would you want to avoid the provided method? I thought that was the whole point of having the class.

        Requirements never cease to amaze.

        Regards,

        Alex.

        Comment

        • puneetsardana88
          New Member
          • Aug 2009
          • 57

          #5
          Originally posted by myusernotyours
          And why would you want to avoid the provided method? I thought that was the whole point of having the class.

          Requirements never cease to amaze.

          Regards,

          Alex.
          Will that become any good thing to ask?

          Anyways thanks everyone for replying......S o how was charAt implemented ...I kw it must have used some other function for its task so on then how that function implemented?

          Comment

          • myusernotyours
            New Member
            • Nov 2007
            • 188

            #6
            Originally posted by puneetsardana88
            Will that become any good thing to ask?

            Anyways thanks everyone for replying......S o how was charAt implemented ...I kw it must have used some other function for its task so on then how that function implemented?
            Hey, don't get touchy. I meant well to ask that question since it sounds strange why any user of a class would want to avoid a provided method unless they have a very good reason to do that.

            Wanting to know how a method is implemented is a totally different matter I think. Maybe I misunderstood what you wanted to do.

            If you simply want to know how charAt is implemented, I believe the JDK install comes with the source code for the String class. Mostly you will find the methods that do not require native implementation. Additionally there is the OpenJDK (Open source java) project which will have the entire sources.

            Regards,

            Alex.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Have you never implemented a String class before?
              I think I must have implemented a String class a thousand times as school assignments or during exams or quizzes at school.

              Anyways, think about what a String really is...it's just an array of chars.

              The String class provides you with methods that add nice functionality which lets you interact with this underlying array of characters.

              So the charAt() method will return you the character at the index in the array of characters that make up the String.

              Comment

              • puneetsardana88
                New Member
                • Aug 2009
                • 57

                #8
                I too have implemented String class in c++ but with the aid of pointers there. In Java Character array is different from String......Act ually what I was thinking how these functions were implemented without existence of pointers.

                Comment

                • myusernotyours
                  New Member
                  • Nov 2007
                  • 188

                  #9
                  Originally posted by puneetsardana88
                  I too have implemented String class in c++ but with the aid of pointers there. In Java Character array is different from String......Act ually what I was thinking how these functions were implemented without existence of pointers.
                  A large part of Java is implemented in C. It's only that this is hidden from the programmer. Therefore there are no pointers in the Java Language, but there are there in the low level implementation of the classes.
                  The same can be said of the other languages on which *there are no pointers*.
                  These languages just makes the programmer's work easier not to have to deal with pointers directly.

                  Regards,

                  Alex.

                  Comment

                  • pbrockway2
                    Recognized Expert New Member
                    • Nov 2007
                    • 151

                    #10
                    There seem to be three questions so far:

                    How do I access characters in a String without using String (or other?) methods? - answer: you can't.

                    How do Java implementations provide String's functionality? - answer: read the code. (It seems to me that this is subtle and difficult as the Java language treats Strings somewhat specially and what you end up with is a complex solution to a complex problem involving efficiency and security).

                    How would I implement a string class?

                    The last is quite interesting. It might be as simple as wrapping a char array and providing the methods you want. Or it might be more meaningful. Why do you want a string class? What will it do? A string that's designed for efficient searches (of some particular type) might involve a more elaborate data structure than a simple array. A string that's going to participate in other data structures might have special (eg hashing) requirements. Should the string be mutable?

                    Comment

                    • myusernotyours
                      New Member
                      • Nov 2007
                      • 188

                      #11
                      But what is really a string? What is the abstraction? It is in my opinion there is no more a best way than an array of characters. That's what the machine understands. If we want a more specialized cousin of the string, we do it another way. An example is the StringBuffer class in Java or the more specific StringBuilder.
                      But the common denominator is that every programmer will need the String class without all the bells and whistles. One that he can use *quick and dirty* without worrying about efficient searches, hashing and all. So it's okay to implement a specialized String, but that doesn't make your usual kind of String redundant.

                      Regards,

                      Alex.

                      Comment

                      Working...