This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a Key and Value from a string

 
Sege Stephen
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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.
 
Charles Hildebrant
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
     
    Kel Michaels
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • 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
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24208
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • 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
    • 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
     
    yue zhang
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • 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
    • 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.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You've already asked this last question in another thread.

    Don't ask it here as well.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic