Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String Functions  RSS feed

 
sri laxmi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ! All,
Can any one tell me how to split a string based on particular character like (, or |) and store the values in hash table in JSP.
This is like very urgent. Please help me .
Thanx in advance.
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a snippet of code i did for just this type thing.....I was sending a usernameTo|usernameFrom|date|time string sent to delete multiple emails from a prog.....this will give you an idea of "splitting" them up after that just run them into a hastable with either a loop or anything that flips your cookie. I was building a query (as you can tell)
int numParams= 0;
String name;
String[] values;
Enumeration enum = req.getParameterNames();
if (enum.hasMoreElements())
{
numParams++;
name = (String)enum.nextElement();
values = req.getParameterValues(name);
for (int x = 0 ; x < values.length ; x++)
{
String work = values[x];
int index = work.indexOf('|');
String work2 = work.substring(0, index);
String work3 = work.substring(index +1 , work.length());
index = work3.indexOf('|');
String work4 = work3.substring(0, index);

String work5 = work3.substring(index + 1, work3.length());
index = work5.indexOf('|');
String work6 = work5.substring(0, index);
String work7 = work5.substring(index + 1, work5.length());
query = "UPDATE intMail SET deleted=2 WHERE fromUserName='" +work2+ "' AND toUserName='" +work4+ "' AND dateSent='" +work6+ "' AND timeSent='" +work7+ "'";
stmt.executeUpdate(query);
 
Kenneth Ho
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may use the library "StringTokenizer". I can't remember the code, you can search in API document.
 
shri ganesh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the StringTokenizer to achieve this.This class is available in java.util package.
Here is a sample code:
String strTemp = "Test|Test1|Test2";
String strTest = null;
//in this example '|' is the separator
StringTokenizer strToken = new StringTokenizer(strTemp ,"|");
while(strToken.hasMoreTokens())
{
strTest = strToken.nextToken();
//add it in to the hashtable here
}
hope this helps!!
bye
shrig
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!