Separating a String, how do I do?

Asked

Viewed 143 times

2

I have a:

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

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

  • Is the java tag correct? Shouldn’t it be [tag:android] instead?

  • 1

    @Florida no, I don’t know if there is any different API on Android, but Java is ok and I believe it is enough, can even add another option.

  • 1

    @Florida android can be programmed using other languages, in this case java is correct.

  • 1

    one url.replace("intent://", "").split("/#Intent;package="); would not solve?

2 answers

3


Since you know the contents and particularities of your string, do so

Split the string into two:

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

this separates the url string into a string array "separate" that will contain 2 items, done this, or Voce separates each of these items into two new ones using split and picking up the part that needs it or Voce can pick up 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, 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 until the end and the value 7 takes the string from 7 to the end, in case "=", I believe that the first case, using split is the most succinct.

If there’s a problem, let me know.

1

Try As Follows:

    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];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.