AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Porting code that uses in/out string parameters

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Andreas Koch
Guest





PostPosted: Tue Jun 20, 2006 1:51 am    Post subject: Porting code that uses in/out string parameters Reply with quote



Hi all,

as a java beginner who has googled a bit on the pass
by value/reference and immutable issue with java
paraemters, i wonder how to implement (or most
conveniently replace) following function:

function cuttoken(in/out fullstring:string;separator:string):string;

this function takes a string containing a separated list,
like "one;two;three", returns the part until the first separator,
and removes that parameter from the passed string. So

String test = "one;two;three"
System.out.println(cuttoken(test,";"));
System.out.println(cuttoken(test,";"));
System.out.println(cuttoken(test,";"));
would print
one
two
three

How to do this in java? Or do i have to split this up to
two calls each (part=getpart, remaining=removepart) ?

thanks,
Andreas
Back to top
Oliver Wong
Guest





PostPosted: Tue Jun 20, 2006 2:00 am    Post subject: Re: Porting code that uses in/out string parameters Reply with quote



"Andreas Koch" <nospam (AT) kochandreas (DOT) com> wrote in message
news:e772fm$467$01$1 (AT) news (DOT) t-online.com...
Quote:
Hi all,

as a java beginner who has googled a bit on the pass
by value/reference and immutable issue with java
paraemters, i wonder how to implement (or most
conveniently replace) following function:

function cuttoken(in/out fullstring:string;separator:string):string;

this function takes a string containing a separated list,
like "one;two;three", returns the part until the first separator,
and removes that parameter from the passed string. So

String test = "one;two;three"
System.out.println(cuttoken(test,";"));
System.out.println(cuttoken(test,";"));
System.out.println(cuttoken(test,";"));
would print
one
two
three

How to do this in java? Or do i have to split this up to
two calls each (part=getpart, remaining=removepart) ?

You can use StringTokenizer:

<pseudoCode>
String input = "one;two;tree";
String seperator = ";";
StringTokenizer st = new StringTokenizer(input, seperator);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
</pseudoCode>

or you can play around with regular expressions.

- Oliver
Back to top
Patricia Shanahan
Guest





PostPosted: Tue Jun 20, 2006 6:34 pm    Post subject: Re: Porting code that uses in/out string parameters Reply with quote



Andreas Koch wrote:
Quote:
Hi all,

as a java beginner who has googled a bit on the pass
by value/reference and immutable issue with java
paraemters, i wonder how to implement (or most
conveniently replace) following function:

function cuttoken(in/out fullstring:string;separator:string):string;

this function takes a string containing a separated list,
like "one;two;three", returns the part until the first separator,
and removes that parameter from the passed string. So

String test = "one;two;three"
System.out.println(cuttoken(test,";"));
System.out.println(cuttoken(test,";"));
System.out.println(cuttoken(test,";"));
would print
one
two
three

How to do this in java? Or do i have to split this up to
two calls each (part=getpart, remaining=removepart) ?

thanks,
Andreas


*** DANGER! DANGER! untested code****

(I want to give examples of how it would look with several techniques)

If the separator is always the same for the whole sequence of splits, i
think the best solution is to split it using String's split.

String test = "one;two;three";
String[] testWords = test.split("/;/");
for(int i=0; i<testWords.length; i++){
System.out.println(testWords[i]);
}

If the separator can change, or you may want to stop before parsing out
all the pieces:

1. Use a StringBuffer or StringBuilder, shared between caller and callee:

StringBuffer test = new StringBuffer("one;two;three");
System.out.println(cuttoken(test,";"));
System.out.println(cuttoken(test,";"));
System.out.println(cuttoken(test,";"));

I don't like this, although it is closest to the method you are using,
because I don't like the shared data structure being modified by the callee.

2. Create a new class that remembers the string:

MyCut test = new MyCut("one;two;three");
System.out.println(test.cutToken(";");
System.out.println(test.cutToken(";");
System.out.println(test.cutToken(";");

A MyCut implementation might stash the string in a StringBuffer or
StringBuilder.


3. More awkward, but a general technique you can use when one method
returns multiple results, use an array result.

String test = "one;two;three";
String[] split;

split = cutToken(test,";");
System.out.println(split[0]);
split = cutToken(split[1],";");
System.out.println(split[0]);
split = cutToken(split[1],";");
System.out.println(split[0]);


Patricia
Back to top
Andreas Koch
Guest





PostPosted: Tue Jun 20, 2006 10:08 pm    Post subject: Re: Porting code that uses in/out string parameters Reply with quote

Quote:
2. Create a new class that remembers the string:

MyCut test = new MyCut("one;two;three");
System.out.println(test.cutToken(";");
System.out.println(test.cutToken(";");
System.out.println(test.cutToken(";");

A MyCut implementation might stash the string in a StringBuffer or
StringBuilder.

Great, that looks like a nice and clean way as the list only
needs setup, cuttoken and empty test.
Too bad i can't subclass the String class directly, but i can
live with a standalone class (using a instance String variable
for storage).

thanks,
Andreas
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.