End of String

Discussion in 'Java' started by sara, Sep 25, 2006.

  1. sara

    sara Guest

    Hi All,

    Is there any way to know the end of a string not by using length()
    function?
    e.g. in C the last character is \0

    Thanks.
     
    sara, Sep 25, 2006
    #1
    1. Advertising

  2. Hi Sara,

    There is no end_of_string character like C in Java.

    -cheers,
    Manish

    sara wrote:
    > Hi All,
    >
    > Is there any way to know the end of a string not by using length()
    > function?
    > e.g. in C the last character is \0
    >
    > Thanks.
     
    Manish Pandit, Sep 25, 2006
    #2
    1. Advertising

  3. sara

    sara Guest

    Sorry for correcting previous massage and sending another one. I wonder
    if there is not such a character, is there any way to implement length
    function in Java?

    Manish Pandit wrote:
    > Hi Sara,
    >
    > There is no end_of_string character like C in Java.
    >
    > -cheers,
    > Manish
    >
    > sara wrote:
    > > Hi All,
    > >
    > > Is there any way to know the end of a string not by using length()
    > > function?
    > > e.g. in C the last character is \0
    > >
    > > Thanks.
     
    sara, Sep 25, 2006
    #3
  4. sara wrote:
    > Sorry for correcting previous massage and sending another one. I wonder
    > if there is not such a character, is there any way to implement length
    > function in Java?


    What's wrong with just using the String's length() function?

    Patricia
     
    Patricia Shanahan, Sep 25, 2006
    #4
  5. sara

    Oliver Wong Guest

    "sara" <> wrote in message
    news:...
    > Sorry for correcting previous massage and sending another one. I wonder
    > if there is not such a character, is there any way to implement length
    > function in Java?


    There are many ways to do so, but all of them would be pretty silly in
    comparison to just calling the existing length() method.

    - Oliver
     
    Oliver Wong, Sep 25, 2006
    #5
  6. sara

    sara Guest

    Hi Guys,

    I am very sorry to reply my messages and I know it is very not good.
    The reason for asking such a question is just curiosity. Because I know
    in C++ we can easily implement strlen() function by checking each
    character but in Java I could not get any good solution.
    Oliver Wong wrote:
    > "sara" <> wrote in message
    > news:...
    > > Sorry for correcting previous massage and sending another one. I wonder
    > > if there is not such a character, is there any way to implement length
    > > function in Java?

    >
    > There are many ways to do so, but all of them would be pretty silly in
    > comparison to just calling the existing length() method.
    >
    > - Oliver
     
    sara, Sep 26, 2006
    #6
  7. sara wrote:
    > Hi Guys,
    >
    > I am very sorry to reply my messages and I know it is very not good.
    > The reason for asking such a question is just curiosity. Because I know
    > in C++ we can easily implement strlen() function by checking each
    > character but in Java I could not get any good solution.


    I still don't understand why asking the String how long it is does not
    qualify as a really good solution.

    Traditional C strings have a problem. They are not really string objects
    or structures, but arrays of char. C arrays do not remember their own
    lengths, and in any case the string data may not occupy the whole array.
    That meant C had to have a way of marking the end of the string, and
    made the rather arbitrary choice to use a zero char as delimiter.

    In Java, a String is an object that is designed to represent string
    data. Internally, it represents the data by the combination of a char[]
    reference, a start index, and the count of the number of characters in
    the String. That way, there is no need for a delimiter, and the length()
    method just returns the value of the count, far simpler and more
    efficient than strlen().

    Patricia
     
    Patricia Shanahan, Sep 26, 2006
    #7
  8. sara wrote:
    > I am very sorry to reply my messages and I know it is very not good.
    > The reason for asking such a question is just curiosity. Because I know
    > in C++ we can easily implement strlen() function by checking each
    > character but in Java I could not get any good solution.


    It is already implemented. It is implemented by the String.length()
    function. Use it, and forget about that C stuff. Java is not C.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
     
    Thomas Weidenfeller, Sep 26, 2006
    #8
  9. sara wrote:
    > Hi Guys,
    >
    > I am very sorry to reply my messages and I know it is very not good.
    > The reason for asking such a question is just curiosity. Because I know
    > in C++ we can easily implement strlen() function by checking each
    > character

    Easily?!? Getting the string length by checking *each* character is not
    easy, and hence strlen() is inefficient by design.
    > but in Java I could not get any good solution.

    You can call the length() method of your String object. This method is
    highly efficient because it doesn't need to count the characters, but
    simply returns a private int value stored besides the actual char[]
    array. What could be easier and more efficient?
    By the way: other languages (Pascal for example) have the same string
    concept.

    --
    Thomas
     
    Thomas Fritsch, Sep 26, 2006
    #9
  10. sara

    Ian Wilson Guest

    sara wrote:
    > Hi All,


    Hello sara,

    > Is there any way to know the end of a string not by using length()
    > function?


    No.

    The only way I know of to "know the end of a string" is by using length().

    What don't you like about length()?

    This sounds like the classic problem partitioning error: Asking "How do
    I do X" when you really want to do Y and have assumed that X is the
    right way to achieve Y. I've done this before and I think you may be now.

    We can't help because "X" looks crazy and you haven't explained "Y" -
    what exactly are you trying to achieve?
     
    Ian Wilson, Sep 26, 2006
    #10
  11. sara wrote:
    > Hi All,
    >
    > Is there any way to know the end of a string not by using length()
    > function?
    > e.g. in C the last character is \0
    >
    > Thanks.
    >


    Ditto what everyone else said. There are times, though, when your
    "string" won't be a String, but rather an array of char (or even int) in
    a buffer that you read from some low-level source. The Javanese way to
    handle that situation is to keep separate integers denoting the index
    and size of the valid buffer content at all times. If you really wanted
    to do things the C way, you could try something like this:

    import java.io.PrintWriter;

    public class Main {

    /**
    * Probably useless, put plausible if you're using multiple string
    * classes that do not all implement any common interface having a
    * length() method.
    */
    public static int strlen(String s) {
    return s.length();
    }

    /** Horribly named function, since it doesn't take a String. */
    public static int strlen(char[] buffer)
    throws IllegalArgumentException {

    for(int i = 0; i < buffer.length; ++i) {

    if(buffer == '\u0000') {
    return i;
    }
    }

    throw new IllegalArgumentException(
    "unterminated C-style string");
    }

    private static void init(char[] buffer, String s) {
    for(int i = 0; i < s.length() && i < buffer.length; ++i) {
    buffer = s.charAt(i);
    }
    }

    public static void main(String[] args){
    PrintWriter out = new PrintWriter(System.out, true);

    final int BUFSIZE = 1024;
    char[] buffer = new char[BUFSIZE];

    init(buffer, "hello");

    out.println(strlen("A string."));
    out.println(strlen(buffer));
    }
    }
     
    Jeffrey Schwab, Sep 26, 2006
    #11
  12. Jeffrey Schwab wrote:

    > private static void init(char[] buffer, String s) {
    > for(int i = 0; i < s.length() && i < buffer.length; ++i) {
    > buffer = s.charAt(i);
    > }


    if(i < buffer.length) {
    buffer = '\u0000';
    }
    > }
     
    Jeffrey Schwab, Sep 26, 2006
    #12
  13. sara

    Guest

    In article <>,
    sara <> wrote:
    > Hi Guys,
    >
    > I am very sorry to reply my messages and I know it is very not good.
    > The reason for asking such a question is just curiosity. Because I know
    > in C++ we can easily implement strlen() function by checking each
    > character


    Well, that's if you're using C-style strings (null-terminated arrays
    of characters) in C++ rather than the "string" library class. I'm not
    sure why one would want to do that, other than for compatibility with
    C library functions.

    (I suppose it's actually possible that under the hood C++ "string"
    objects are implemented as C-style strings with some extra baggage,
    and I suppose it's even possible that one can rely on that, in which
    case rolling one's own length function by scanning for a null
    character would work for C++ "string" objects too.)

    Maybe off-topic and/or a nitpick.

    [ snip ]

    --
    B. L. Massingill
    ObDisclaimer: I don't speak for my employers; they return the favor.
     
    , Sep 26, 2006
    #13
  14. [OT] Re: End of String

    wrote:
    > In article <>,
    > sara <> wrote:
    >> Hi Guys,
    >>
    >> I am very sorry to reply my messages and I know it is very not good.
    >> The reason for asking such a question is just curiosity. Because I know
    >> in C++ we can easily implement strlen() function by checking each
    >> character

    >
    > Well, that's if you're using C-style strings (null-terminated arrays
    > of characters) in C++ rather than the "string" library class. I'm not
    > sure why one would want to do that, other than for compatibility with
    > C library functions.
    >
    > (I suppose it's actually possible that under the hood C++ "string"
    > objects are implemented as C-style strings with some extra baggage,
    > and I suppose it's even possible that one can rely on that, in which
    > case rolling one's own length function by scanning for a null
    > character would work for C++ "string" objects too.)


    Some do it that way, some don't. Even the ones that do are usually not
    quite that simple, since std::string uses COW. The c_str() method is
    provided for compatibility.
     
    Jeffrey Schwab, Sep 26, 2006
    #14
  15. sara

    Javier Guest

    Jeffrey Schwab ha escrito:


    > /** Horribly named function, since it doesn't take a String. */
    > public static int strlen(char[] buffer)
    > throws IllegalArgumentException {
    >
    > for(int i = 0; i < buffer.length; ++i) {
    >
    > if(buffer == '\u0000') {
    > return i;
    > }
    > }



    Even in this case Java would be easier than C:

    public static int strlen(char[] buffer){return buffer.length;}

    or simply, given a char buffer, just access the length of it with
    buffer.length, without the need of using that horrible named function.
    :)

    Remember that arrays in Java _also_ have the length attribute.
     
    Javier, Sep 26, 2006
    #15
  16. Javier wrote:
    > Jeffrey Schwab ha escrito:
    >
    >
    >> /** Horribly named function, since it doesn't take a String. */
    >> public static int strlen(char[] buffer)
    >> throws IllegalArgumentException {
    >>
    >> for(int i = 0; i < buffer.length; ++i) {
    >>
    >> if(buffer == '\u0000') {
    >> return i;
    >> }
    >> }

    >
    >
    > Even in this case Java would be easier than C:
    >
    > public static int strlen(char[] buffer){return buffer.length;}
    >
    > or simply, given a char buffer, just access the length of it with
    > buffer.length, without the need of using that horrible named function.
    > :)
    >
    > Remember that arrays in Java _also_ have the length attribute.


    That's not the same thing. What's of interest here is not the size of
    the buffer, but the amount of valid content it contains.
     
    Jeffrey Schwab, Sep 26, 2006
    #16
  17. sara

    Nigel Wade Guest

    sara wrote:

    > Hi Guys,
    >
    > I am very sorry to reply my messages and I know it is very not good.


    Replying to others who have responded to you is the norm.

    > The reason for asking such a question is just curiosity. Because I know
    > in C++ we can easily implement strlen() function by checking each
    > character but in Java I could not get any good solution.


    In Java it's very easy:

    static int strlen(String str) {
    return str.length();
    }

    Even more pointless, but not using length() per your original request :

    static int strlen(String str) {
    int i=0;
    try {
    while(true) {
    str.getChar(i);
    i++;
    }
    }
    catch(IndexOutOfBoundsException) {
    return i;
    }
    }

    Are they enough to meet your requirements?

    HINT: there is no need to implement a strlen "function" in Java because it is
    not necessary in the way that it is in C/C++. A String in Java is an object,
    and a part of that object is its own length, and a method to access it.

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, Sep 26, 2006
    #17
  18. sara

    Javier Guest

    Jeffrey Schwab ha escrito:


    > > Remember that arrays in Java _also_ have the length attribute.

    >
    > That's not the same thing. What's of interest here is not the size of
    > the buffer, but the amount of valid content it contains.


    Then you'd better use StringBuffer instead:

    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

    and having the code \0000 to delimit the valid content of a buffer is
    not always a good idea. There may be valid content beyond that.
     
    Javier, Sep 26, 2006
    #18
  19. sara

    Oliver Wong Guest

    "Nigel Wade" <> wrote in message
    news:efbjq8$p74$...
    >
    > Even more pointless, but not using length() per your original request :
    >
    > static int strlen(String str) {
    > int i=0;
    > try {
    > while(true) {
    > str.getChar(i);
    > i++;
    > }
    > }
    > catch(IndexOutOfBoundsException) {
    > return i;
    > }
    > }
    >
    > Are they enough to meet your requirements?


    I was trying to come up with the "most pointless" way to solve the
    requirement, in case Sara persisted.

    Best I came up with is using a recursive method which generates every
    single possible string of known length, and checks whether the generated
    string matches the provided string.

    <pseudoCode>
    int getStringLength(String target) {
    int length = 0;
    while(true) {
    if (stringEquals(target, "", length)) {
    return length;
    }
    length++;
    }
    }

    boolean stringEquals(String target, String prefix, int length) {
    if (length == 0) {
    return target.equals(prefix);
    }
    for (char nextChar : allPossibleCharacters()) {
    if (stringEquals(target, prefix + nextChar, length - 1) {
    return true;
    }
    }
    return false;
    }
    </pseudoCode>

    - Oliver
     
    Oliver Wong, Sep 26, 2006
    #19
  20. sara

    Oliver Wong Guest

    "Javier" <> wrote in message
    news:...
    >
    > Jeffrey Schwab ha escrito:
    >
    >
    >> > Remember that arrays in Java _also_ have the length attribute.

    >>
    >> That's not the same thing. What's of interest here is not the size of
    >> the buffer, but the amount of valid content it contains.

    >
    > Then you'd better use StringBuffer instead:
    >
    > http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html
    >
    > and having the code \0000 to delimit the valid content of a buffer is
    > not always a good idea. There may be valid content beyond that.


    The context of the thread was emulating C's behaviour. The closest thing
    to having pointings to arbitrary contiguous regions of memory in Java is
    allocating an array, and C uses \u0000 to delimit the end of Strings.

    - Oliver
     
    Oliver Wong, Sep 26, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo Geshel
    Replies:
    2
    Views:
    3,720
    Versteijn
    Aug 18, 2004
  2. William
    Replies:
    2
    Views:
    1,804
    Kevin Spencer
    Jun 1, 2005
  3. Replies:
    2
    Views:
    367
  4. Jimmy
    Replies:
    3
    Views:
    356
    J. J. Farrell
    Sep 9, 2011
  5. Jimmy
    Replies:
    13
    Views:
    672
    Peter Nilsson
    Sep 21, 2011
Loading...

Share This Page