1

Say I have this string :

/mytea/en/bt/aer?taxYear=2021&startMonth=1&endMonth=1

There are three parameters there :

  • taxYear : 4 digits => like "2021"
  • startMonth : 1 digit or 2 digits => from 1 to 12
  • endMonth: 1 digit or 2 digits => from 1 to 12

How can I do to extract these parameters from the string in Java or Scala?


I have already tried this :

 public static void main(String []args){
     String hello = "/mytea/en/bt/aer?taxYear=2021&startMonth=1&endMonth=1";
     String test = hello.substring(52, 53);
    System.out.println(test);
 }

And it returns "1", while test = endMonth.

However if I change the value of endMonth in the url by "10", it still returns "1", and not 10.

2
  • 5
    The easiest way would be to use java.net.URL. Commented Dec 15, 2021 at 16:56
  • 1
    "However if I change the value of endMonth in the url by "10", it still returns "1", and not 10." - Because hello.substring(52, 53) only parses out one character. The correct way to get around this is to use indexOf when practical: For example: hello.substring(X hello.indexOf("someSubstring")), or use hello.substring(52) to go all the way to the end of the string. Commented Dec 15, 2021 at 17:07

1 Answer 1

2

Use the String.split method to split up the string based upon the delimiters. It takes a couple of steps to do this, but basically you want to do something like this:

import java.util.HashMap;

public class StringExtract {

    public static void main(String[] args) {
        String s = "/mytea/en/bt/aer?taxYear=2021&startMonth=1&endMonth=1";

        String[] a = s.split("\\?");    // Split string around '?' character
        System.out.println(a[0] + "  " + a[1]);
        String[] b = a[1].split("&");   // Split string around '&' character

        HashMap<String, String> map = new HashMap<>();
        // For each name=value pair, split the string around '=' and print it out
        for(int i=0; i<b.length; i++) {
            String[] values = b[i].split("=");
            System.out.println("Name: " + values[0] + " Value: " + values[1]);
            map.put(values[0], values[1]); // store name/value pair in map
        }

        // Save values in variables
        int taxYear = Integer.parseInt(map.get("taxYear"));
        int startMonth = Integer.parseInt(map.get("startMonth"));
        int endMonth = Integer.parseInt(map.get("endMonth"));


    }
}

Outputs:

/mytea/en/bt/aer  taxYear=2021&startMonth=1&endMonth=1
Name: taxYear Values: 2021
Name: startMonth Values: 1
Name: endMonth Values: 1
Sign up to request clarification or add additional context in comments.

6 Comments

I would replace your String#split() with String.substring() using indexOf() to delimit the obtained substring. It would be a much cleaner solution in my opinion. But, I still upvoted your answer.
@hfontanez That would work too. It didn't occur to me to do that. :) I saw the delimiters and automatically thought of String#split().
@M.Gianota I was wondering, do you how to do to save these printed values in separated variables? Like taxYear, startMonth and endMonth?
@finalmix6 If you put the name/value pairs stored in the values array into a HashMap object (map.put(values[0], values[1]) then you can retrieve them by name and set a variable: int taxYear = Integer.parseInt(map.get("taxYear")); And so on for the other two variables.
@finalmix6 I updated the answer to show the code for storing the name/value pairs in variables.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.