aspose file tools*
The moose likes Beginning Java and the fly likes Getting a Key and Value from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting a Key and Value from a string" Watch "Getting a Key and Value from a string" New topic
Author

Getting a Key and Value from a string

Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
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

Joined: Sep 28, 2007
Posts: 7
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.


    Cheers<br />Charles<br /> <br />Sometimes we live no particular way but our own.<br />~ Robert Hunter, Grateful Dead
    Kel Michaels
    Greenhorn

    Joined: Nov 28, 2006
    Posts: 5
    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

    Joined: Jul 08, 2003
    Posts: 24184
        
      34

    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.


    [Jess in Action][AskingGoodQuestions]
    Sege Stephen
    Ranch Hand

    Joined: Oct 06, 2007
    Posts: 51
    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

    Joined: May 15, 2006
    Posts: 5
    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

    Joined: Oct 06, 2007
    Posts: 51
    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

    Joined: Aug 05, 2005
    Posts: 3566
        
      15
    You've already asked this last question in another thread.

    Don't ask it here as well.


    Joanne
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Getting a Key and Value from a string