如何提取/重组字符串值

How to extract / restructure a string value

我在变量中有以下可能的值:URL,在 JAVA/JSP.

URL="http://a.b.com/Something?IDToken1=AppUser1"
URL="http://a.b.com/Something?IDToken1=AppUser1&variable1=value1"
URL="http://a.b.com/Something?IDToken1=AppUser1variable1=value1&variable2=value2"

URL="http://a.b.com/Something?variable1=value1&IDToken1=AppUser1"
URL="http://a.b.com/Something?variable1=value1&IDToken1=AppUser1&variable2=value2"

我试图找到我可以使用字符串或相关函数编写哪些代码(一行或 2-5 行),这些函数可以给我一个名为:"user=AppUser1" 和 reConstructedURL=上面的任何值 link(如果 IDToken1 是传递给 URL 的唯一参数,则从中删除 IDToken1=AppUser1 后除外)否则,保留“ ?" 字符和所有其他参数(+ 排除第一个 & 字符以及即没有 & character/value 在 reConstructedURL 变量中作为结束符)。我尝试了函数的子字符串和索引,但无法正常工作。

例如:

reConstructedURL="http://a.b.com/Something"

if URL 是:
URL="http://a.b.com/Something?IDToken1=AppUser1"


reConstructedURL="http://a.b.com/Something?variable1=value1"

if URL 是:
URL="http://a.b.com/Something?IDToken1=AppUser1&variable1=value1"




reConstructedURL="http://a.b.com/Something?variable1=value1&variable2=value2"

if URL 是:
URL="http://a.b.com/Something?IDToken1=AppUser1&variable1=value1&variable2=value2"




reConstructedURL="http://a.b.com/Something?variable1=value1"

if URL 是:
URL="http://a.b.com/Something?variable1=value1&IDToken1=AppUser1"




reConstructedURL="http://a.b.com/Something?variable1=value1&variable2=value2"

if URL 是:
URL="http://a.b.com/Something?variable1=value1&IDToken1=AppUser1&variable2=value2"

这是我目前尝试过的方法。

   public final String USER_VAR = "IDToken1";
   public String[] extractNewURL(String currentGoToURL)
   {
       String[] userAndRetURL = new String[2];
       //Sample incoming
       //http://a.b.com/Something?IDToken1=Apptester2
       //http://a.b.com/Something?IDToken1=Apptester2&variable2=100
       //http://a.b.com/Something?variable1=Giga&IDToken1=Apptester2&variable2=100
       try
       {
           String extractedUsername = "";
           String[] stringArray = currentGoToURL.split(USER_VAR+"=");
           String usernamePlusPlus = stringArray[1];
           //Go up to the &
           int firstAmp = usernamePlusPlus.indexOf('&');
           if(firstAmp != -1)
           {
               extractedUsername = usernamePlusPlus.substring(0, firstAmp);
           }
           else
           {
               extractedUsername = usernamePlusPlus;
           }
           userAndRetURL[0] = extractedUsername;

           //2nd part to reconstruct the URL
           if(!"".equals(extractedUsername))
           {
               //TODO this needs to be smarter to pass along extra params
               String[] splitURL = currentGoToURL.split("\?");
               String beginningURL = splitURL[0];
               //System.err.println("BEG:"+beginningURL);
               userAndRetURL[1] = beginningURL;
           }
           else
           {
               userAndRetURL[1] = currentGoToURL;
           }

       } catch (Exception e) {
           //Ends up in catalina.out
           System.err.println("EXCEPTION OCCURRED IN LOGIN.JSP:  "+e.toString());
           e.printStackTrace();
       }

       return userAndRetURL;
   }
        %>
            <%
        boolean weHaveUser = false;
        String userNameCarryFwd = "DummyUser";
        //App_USERNAME info is present in Headers
        userNameCarryFwd = response.getHeader("App_USERNAME");
        if (userNameCarryFwd == null)
        {
            userNameCarryFwd = request.getParameter("App_USERNAME");
            if(userNameCarryFwd == null) 
            {
                userNameCarryFwd = request.getParameter("IDToken1");
                if(userNameCarryFwd == null) 
                {
                    //This is crazy but we are passing it on the URL to get around header not fwding
                    String somethingToParse = request.getParameter("goto");
                    String[] uAndURL = extractNewURL(somethingToParse);
                    userNameCarryFwd = uAndURL[0];
                    String nonEncoded = uAndURL[1];
                    //This will be after reconstructive surgery
                    gotoURL = viewBean.getEncodedInputValue(nonEncoded);

                    if(userNameCarryFwd == null) 
                        userNameCarryFwd = "";
                }
            }
        }
        weHaveUser = (userNameCarryFwd == null || userNameCarryFwd.length() < 1) ? false : true;

我认为最好的逻辑理解方式是: 1. 从当前 Go To URL 字符串中,如果我可以去掉 IDToken=< value > 部分并确保结果 "redirected URL" 字符串的结束字符不以 - ?& 结尾,即删除 ?& 如果那是去掉上面 variable=value 部分后的最后一个字符 and,如果 ?&&&连续字符存在,则用?&字符替换,即做吧。

下面的代码可以帮到你。

public class URLResonstruction {


    public static String reconstructURL(String url) {
        int beginTknInx = url.indexOf("IDToken1");

        // If there is no IDToken
        if (beginTknInx < 0)
            return url;

        // If IDToken is only query parameter or IDToken is at end
        int endTknInx = url.substring(beginTknInx).indexOf("&");
        if (endTknInx < 0)
            return url.substring(0, beginTknInx - 1);

        // If IDToken is at beginning or in middle
        String part1 = url.substring(0, beginTknInx);
        String part2 = url.substring(beginTknInx + endTknInx);
        return part1 + part2.substring(1, part2.length());
    }
}