I can only get part of a url (link) and put into string in android?

后端 未结 2 1498
走了就别回头了
走了就别回头了 2020-12-22 13:48

[Solved] - I need to resolve the problem below Ok I wasn\'t really sure how to word this question, but basically what I want to do is, I g

2条回答
  •  借酒劲吻你
    2020-12-22 14:19

    You can achieve that making a substring of the number you want to find and then appending that to the string you need.

    The code would be as follows:

        //the original String
        String somestring = "http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821";
        //save the index of the string '=' since after that is were you find your number, remember to add one as the begin index is inclusive
        int beginIndex = somestring.indexOf("=") + 1;
        //if the number ends the string then save the length of the string as the end, you can change this index if that's not the case
        int endIndex = somestring.length();
        //Obtain the substring using the indexes you obtained (if the number ends the string you can ignore the second index, but i leave it here so you may use it if that's not the case)
        String theNumber = somestring.substring(beginIndex,endIndex);
        //printing the number for testing purposes
        System.out.println("The number is: " + theNumber);
        //Then create a new string with the data you want (I recommend using StringBuilder) with the first part of what you want
        StringBuilder sb=new StringBuilder("http://shake.uprm.edu/~shake/archive/shake/");
        // add the number
        sb.append(theNumber);
        //then the rest of the string
        sb.append("/download/tvmap.jpg");
        //Saving the String in a variable
        String endResult = sb.toString();
        //Verifying end result 
        System.out.println("The end result is: "+endResult);
    

    After that you can put the String into Glide I hope It was clear and well explained.

提交回复
热议问题