1

I tried to get string from long string which is Firebase URL

"https://firebasestorage.googleapis.com/v0/b/No-manworld-3577.appspot.com/o/Contacts%2F1510361061636_Julien_Vcf?alt=media&token=c0bff20d-d115-4fef-b58c-4c7ffaef4296"

Now if you notice there is under score before and after name Julien in above string. I am trying to get that name but i am getting

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Here is my piece of code

String s="https://firebasestorage.googleapis.com/v0/b/No-manworld-3577.appspot.com/o/Contacts%2F1510361061636_Julien_Vcf?alt=media&token=c0bff20d-d115-4fef-b58c-4c7ffaef4296";

        String newName=s.substring(s.indexOf("_")+1, s.indexOf("_"));
        System.out.println(newName);
Ritu
  • 518
  • 2
  • 12
  • 35
  • When using substring, the first number has to be smaller than the second one. In your case, you are calling substring with `x + 1` and `x`. `x + 1 > x` thus substring fails. `-- EDIT` x being `s.indexOf("_")` – nicovank Nov 11 '17 at 01:26
  • @nicovank Can you elaborate your comment as answer – Ritu Nov 11 '17 at 01:27
  • `s.indexOf("_")` is 99. You are basically calling `s.substring(99 + 1, 99)`, whereas the second argument of substring has to be bigger than the first one. – nicovank Nov 11 '17 at 01:29
  • I changed `(s.indexOf("_"), s.indexOf("_")+1)` but now result is blank. How can i get name Julien as string – Ritu Nov 11 '17 at 01:32

2 Answers2

2

As said in my comment, when using substring, the first number has to be smaller than the second one.

In your case, you are calling substring with x + 1 and x. x + 1 > x thus substring fails, with x being s.indexOf("_").

I understand that you are trying to get the second indexOf of _.

Here is code that would in your case yield Julien:

String s = "...";

int start = s.indexOf("_") + 1;
int end = s.indexOf("_", start);

// name will hold the content of s between the first two `_`s, assuming they exist.
String name = s.substring(start, end);
nicovank
  • 3,157
  • 1
  • 21
  • 42
  • If you see lot of questions and answers related to this issue (Same as i tried) but its not work in my case. This thing is lil tricky and might be helpful for others too like me. Thanks alot – Ritu Nov 11 '17 at 01:45
0

If requirements are not clear on which 2 _ to select then here is Java 8 Stream way of doing it ..

public class Check {
    public static void main(String[] args) {
        String s = "https://firebasestorage.googleapis.com/v0/b/No-manworld-3577.appspot.com/o/Contacts%2F1510361061636_Julien_Vcf?alt=media&token=c0bff20d-d115-4fef-b58c-4c7ffaef4296";
        long count = s.chars().filter(ch -> ch == '_').count();
        if (count == 2) {
            System.out.println(s.substring(s.indexOf('_') + 1, s.lastIndexOf('_')));
        } else {
            System.out.println("More than 2 underscores");
        }
    }
}

Why your code didn't work?

Let assume s.indexOf("_") gets some positive number say 10 then below translates to ...

String newName=s.substring(s.indexOf("_")+1, s.indexOf("_"));

String newName=s.substring(11, 10);

This will give StringIndexOutOfBoundsException as endIndex < beginIndex for subString method.

JRG
  • 4,037
  • 3
  • 23
  • 34