• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Getting a Key and Value from a string

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have this code that return the user info below, I only need one part of the information returned, how do I get that value and pass it in my code?


String header = request.getHeader("CSP-USer-Info");
if (header != null) {}
out.println("CSP-User-Info: " + header +"<br/>");


this code returns

CSP-User-Info: AccountID=250387,Login%u0020Name=sampath,UserId=79


I need only need the UserID= part . How do I do it. If it was in a map, I could do something like header.get("UserID"). But it wouldn't allow me to do that, how do I get round it. I need to pass the USerID somewhere in my code.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a greenhorn at this as well but, based on my javaScript and C++ experience, I'd have you look into two String functions.
  • indexOf()
  • subString()

  • I think these may prove helpful. The indexOf() finds where the string you want to see starts and subString() grabs the string you want based on the result of indexOf().

    See if that at least gets you started.
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Is this in a servlet? If so...

    If you just want to get a parameter from a request form/session you can use request.getParameter("UserId") this will return a string with that parameter. You don't need to get the full header.

    The API for request is at http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html
     
    author and iconoclast
    Posts: 24207
    46
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't think Kel's suggestion is appropriate here.

    substring and indexOf() would be one way to do this; the newer String.split() method would be another. Splitting several times, using ',' and then '=' as separator, would let you break this into key, value pairs, and then into individual keys and values.
     
    Sege Stephen
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Guys for your replies, You actually pointed me in the right direction. Using String Tokenizer, split and indexes are the way to do it.

    Thanks
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    CSP-User-Info: AccountID=250387,Login%u0020Name=sampath,UserId=79
    =========
    1. use String.split(",") split the String to a String array
    2. then conversion the String array to a HashMap(key, value),(you need write a method by yourself.)
    3. use HashMap's get(Object key) method get what you want value

    4. then you can reuse this method in other class
     
    Sege Stephen
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Guys, I was able to get the string and then I had to break it down using String tokenizer. The value I want at this time is the one stored in String f4 which is the value of the user ID.

    I need this f4 in another part of my code , how do I get it.



    public class Tokens {



    StringTokenizer token1;

    String x = "AccountID=250387,Login%u0020Name=sampath,UserId=79";
    token1 = new StringTokenizer(x,"=");
    String f1 = token1.nextToken();
    String f2 = token1.nextToken();
    String f3 = token1.nextToken();
    String f4 = token1.nextToken();



    Now I have tried doing something like this in another class

    public void setF4(String f4){
    this.f4=f4;
    }

    public String getF4(){
    return f4;
    }

    then I created a new instance of the class Tokens, but it doesn't work.

    How do I retrieve and use this value stored in f4 in a another class.

    Very easy, but I can't do it. Thanks for your help.
     
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You've already asked this last question in another thread.

    Don't ask it here as well.
     
    Uh oh, we're definitely being carded. Here, show him this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic