Separating a String, how do I?

2

I have one:

String  url = intent://instagram.com/_u/fabiohcnobre/#Intent;package=com.instagram.android;scheme=https;end

How do I separate it so that I get a String link = instagram.com/_u/fabiohcnobre and a String package = com.instagram.android

    
asked by anonymous 29.10.2016 / 22:19

2 answers

3

As you know the content and peculiarities of your string, do so

Separate the string into two:

String [] separada = url.split(";");

This separates the url string into an array of " separate " string that will contain 2 items, done this, or you separate each of these items into two new ones using split and picking up the part you need or you can get the sub string of each of these items, something like this:

String link = separada[0].split("//")[1];
String pack = separada[1].split("=")[1];

or

String link = separada[0].substring(8,separada[0].lenght());
String pack = separada[1].substring(7,separada[1].lenght());

I think this is it, remembering that the value 8 of the first case is the value that starts counting the string from 0 to "//", that is, takes the string after them to the end and the value 7 the string from 7 until the end, in case "=", I believe the first case, using split is the most succinct.

If there is any problem, let me know.

    
29.10.2016 / 22:44
1

Try the following way:

    String  url = "intent://instagram.com/_u/fabiohcnobre/#Intent;package=com.instagram.android;scheme=https;end";

    // Quebramos os valores onde há ;
    String [] vals = url.split(";");
    // O que nos interessa!!
    String intent  = vals[0];
    String link  = vals[1];

    //Trocamos os valores desnecessários...
    String cleanIntent = intent.replaceAll("#Intent", "").replaceAll("intent://", "");
    //Quebramos o link onde há = e pegamos o segundo item
    String cleanLink = link.split("=")[1];
    
01.11.2016 / 17:52