• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string delimiter

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Kicky San
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any suggestions on this?
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic