wood burning stoves*
The moose likes Java in General and the fly likes string delimiter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "string delimiter" Watch "string delimiter" New topic
Author

string delimiter

Kicky San
Greenhorn

Joined: Aug 24, 2005
Posts: 18
Hi Ranchers,
I have got a question on string delimiters.

I am working on a java script which takes user input from two text areas and sends it to a servlet.
I would like to concatenate as a single string in the javascript and send it to the servlet through POST method.
The string values entered can be of any language. In servlet, the strings are converted to UTF-8 format. Then I pass the whole string to a database procedure where it is splitted.

What delimiter is the best? (I thought of using | and ^) - but am scared about the encoding part of the whole string.Will there be any problems in using these characters? For example can two japanese/chinese/spanish strings be concatenated with these delimiters without any problem?

Any help would be highly appreciated.

Thanks !


Cheers,<br />KicKy
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
Instead of using a delimiter, you could measure the length of each String, and use e.g. the first five digits to denote the length of the following String, like this:

First string: "First string" (length 12)
Second string: "Second string" (length 13)

Concatenated string: "00012First string00013Second string"

Then, on the receiving side, you parse 00012 into an int, finding the length of the first string, and repeat for the second. You don't really need the second, but doing so allows you to easily expand into three or more strings later.


Entia non sunt multiplicanda praeter necessitatem
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I've made a routine in several languages over the years to search through the values to be concatenated for a character that is not present in any of them. That gives a safe delimiter. Then to let the splitter know what the delimiter is, code it first. So the result might look like:

/one/two/

or even

aoneatwoa

I actually like the length suggestion better as it skips the searching part. You could delimit the digits with any non-digit to allow variable length numbers:

3-one11-banana split

And before we go, we should ask why we're concatenating and splitting in the first place. Can you make two values look like two form fields in the POST?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Kicky San
Greenhorn

Joined: Aug 24, 2005
Posts: 18
Thanks for your suggestions.

In this case, I have access only to javascript file and database procedure.
I am not supposed to change the servlet.
The servlet, now, takes in only one string, trims it, converts it into a byte array and forms a new String depending on the language charset.

'feedback' is the string I get from the javascript.

byte[] byteArray = feedback.getBytes("ISO-8859-1");
try { //check to make sure charset encoding is supported
feedback = new String(byteArray, docCharset);
}

After this step the feedback string is passed into the database procedure.

- delimiting with length seems to be plausible. but what if the user inputs some numbers along with the string.
eg: user inputs "12 drivers are present" and "dsfds" in the text area in frontend.

- There are not only two user input boxes as I mentioned early. But there are 6 boxes.

- Would this delimiter '�' solve the problem?
(i.e.,) when 서비스센터 안내 � 잉크 구입처 안내 is sent into the servlet,
while converting this whole string as mentioned in the above snippet, will there be any problem?
Kicky San
Greenhorn

Joined: Aug 24, 2005
Posts: 18
Any suggestions on this?
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
Originally posted by Kicky San:
- delimiting with length seems to be plausible. but what if the user inputs some numbers along with the string.
eg: user inputs "12 drivers are present" and "dsfds" in the text area in frontend.


Doesn't matter. If you go for my original idea, you take the first 5 characters (or whatever length you decide upon), and parse them to an int to get the lengt. You then take the substring from position 5 to position 5+<length>, which is your first string. From this position you then take the next 5 characters, parse them, etc...

If you go for Stan's modification of this, you start at 0, get the index of the next instance of a predefined character (e.g. "-"), parse this to an int to get the end of the first substring, get that, and from the end of that you repeat.

Like this:

Writing the strings to the "superstring":


send the StringBuffer as a String to the servlet

Extracting the strings from the superstring:


Probably, your database is not coded in java, so the syntax is probably different.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string delimiter
 
Similar Threads
Java APIs for Text file comparison
About tokenizer.
string delimiter
Java equivalent for Perl's join ? ( concat Collection w/separator )
Tokenizing with regex pattern. Little confused!