End of String

S

sara

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.
 
S

sara

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

Patricia Shanahan

sara said:
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
 
O

Oliver Wong

sara said:
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
 
S

sara

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.
 
P

Patricia Shanahan

sara said:
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
 
T

Thomas Weidenfeller

sara said:
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
 
T

Thomas Fritsch

sara said:
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.
 
I

Ian Wilson

sara said:

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

Jeffrey Schwab

sara said:
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));
}
}
 
J

Jeffrey Schwab

Jeffrey said:
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';
}
 
B

blmblm

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 ]
 
J

Jeffrey Schwab

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.
 
J

Javier

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.
 
J

Jeffrey Schwab

Javier said:
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.
 
N

Nigel Wade

sara said:
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.
 
O

Oliver Wong

Nigel Wade said:
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
 
O

Oliver Wong

Javier said:
Jeffrey Schwab ha escrito:



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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top