Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Converting String to an array.

 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a String like
String param = "invoice number, file name, userid";
How do i convert this into an array and extract each values,
My String param can contain sometimes 3 words(with comma separated) or sometimes 5 or 2 , i get param dynamically.
can anyone suggest what to do
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
what you can do is the following:

HIH
Val
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excuse me I think I did not quite answer your question...
The best way to extract tokens from a String object is with a StringTokenizer object, for instance:

All tokens of the initial String are now contained in the String array named "tokens".
HIH
Val
 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want my array to be like this
String arr[] = {"invoice number", "file name", "userid" }
not individual characters
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this code prints:
invoice number
file name
userid


hope helps
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops...
I din't see the previous post
 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys
i got it
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you do a toCharArray the commas are also stored in the elements to avoid this use StringTokenizer with comma as the delimiter.
For example:
String[] s = new String[10];
String param = "invoice number, file name, userid";
StringTokenizer st = new StringTokenizer(param,",");
int i = 0;
while (st.hasMoreTokens())
{
s[i]= st.nextToken();
System.out.println("String[] elements:"+s[i]);
i++;
}
I hope your requirements were met.
Roopa.

Originally posted by Bhasker Reddy:
I have a String like
String param = "invoice number, file name, userid";
How do i convert this into an array and extract each values,
My String param can contain sometimes 3 words(with comma separated) or sometimes 5 or 2 , i get param dynamically.
can anyone suggest what to do


 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic