Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > End of String

Reply
Thread Tools

End of String

 
 
Jeffrey Schwab
Guest
Posts: n/a
 
      09-26-2006
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[i] == '\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[i] = 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));
}
}
 
Reply With Quote
 
 
 
 
Jeffrey Schwab
Guest
Posts: n/a
 
      09-26-2006
Jeffrey Schwab wrote:

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


if(i < buffer.length) {
buffer[i] = '\u0000';
}
> }

 
Reply With Quote
 
 
 
 
blmblm@myrealbox.com
Guest
Posts: n/a
 
      09-26-2006
In article <(E-Mail Removed). com>,
sara <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      09-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> In article <(E-Mail Removed). com>,
> sara <(E-Mail Removed)> 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.
 
Reply With Quote
 
Javier
Guest
Posts: n/a
 
      09-26-2006

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[i] == '\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.

 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      09-26-2006
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[i] == '\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.
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      09-26-2006
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 : (E-Mail Removed)
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
Reply With Quote
 
Javier
Guest
Posts: n/a
 
      09-26-2006

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/...ingBuffer.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.

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      09-26-2006
"Nigel Wade" <(E-Mail Removed)> wrote in message
news:efbjq8$p74$(E-Mail Removed)...
>
> 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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      09-26-2006

"Javier" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> 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/...ingBuffer.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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to match string end for a multiline string? Peng Yu Perl Misc 2 06-24-2008 02:32 AM
Help with switch configuration, ( 3 3550's, 5 2950's end to end ) ec Cisco 3 07-25-2006 10:30 AM
String Gets Truncated: VBScript End of String Character? Mike ASP General 1 08-25-2005 02:50 PM
Measure delay end-to-end Dave Cisco 1 07-20-2004 12:51 PM
is there a difference between CIR and CIR+end to end clear channel connection? ike lozada Cisco 0 05-27-2004 02:34 AM



Advertisments