1

I have string

 String path =    /mnt/sdcard/Album/album3_137213136.jpg

I want to only strings album3. How can I get that substring.

I am using substring through index. Is there any other way because album number is getting changed because it will fail in like album9, album10.

2
  • 2
    How about Regular expressions ? Commented Jun 30, 2013 at 18:27
  • @kocko yeah.. Good one bro!!! Commented Jun 30, 2013 at 18:34

7 Answers 7

5

You can use a regular expression, but it seems like using index is the simplest in this case:

int start = path.lastIndexOf('/') + 1;
int end = path.lastIndexOf('_');
String album = path.substring(start, end);

You might want to throw in some error checking in case the formatting assumptions are violated.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this

public static void main(String args[]) {
    String path =   "/mnt/sdcard/Album/album3_137213136.jpg";
    String[] subString=path.split("/");
    for(String i:subString){
          if(i.contains("album")){
              System.out.println(i.split("_")[0]);
          }
    }
}

Comments

1

Obligatory regex solution using String.replaceAll:

String album = path.replaceAll(".*(album\\d+)_.*", "$1");

Use of it:

String path = "/mnt/sdcard/Album/album3_137213136.jpg";
String album = path.replaceAll(".*(album\\d+)_.*", "$1");
System.out.println(album); // prints "album3"
path = "/mnt/sdcard/Album/album21_137213136.jpg";
album = path.replaceAll(".*(album\\d+)_.*", "$1");
System.out.println(album); // prints "album21"

Comments

1

Using Paths:

final String s = Paths.get("/mnt/sdcard/Album/album3_137213136.jpg")
    .getFileName().toString();
s.subString(0, s.indexOf('_'));

If you don't have Java 7, you have to resort to File:

final String s = new File("/mnt/sdcard/Album/album3_137213136.jpg").getName();         
s.subString(0, s.indexOf('_'));

2 Comments

Doesn't this assume that the file separator character is '/'?
Well yes, but it seems the OP is on Android; so it is quite a safe bet, I guess ;)
0

Use regex to match the substring

path.matches(".*album[0-9]+.*")

Comments

0

Try this ..

 String path =    /mnt/sdcard/Album/album3_137213136.jpg
    path = path.subString(path.lastIndexOf("/")+1,path.indexOf("_"));
    System.out.println(path);

Comments

0

How to count substring in String in java

At line no 8 we have to used for loop another optional case replace for loop using just while loop like as while(true){. . .}

public class SubString {
    public static void main(String[] args) {
    int count = 0 ;
    
    String string = "hidaya: swap the Ga of Gates with the hidaya: of Bill to make Bites."
            + " The hidaya: of Bill will then be swapped hidaya: with the Ga of Gates to make Gall."
            + " The new hidaya: printed out would be Gall Bites";
    for (int i = 0; i < string.length(); i++) 
    {
        int found = string.indexOf("hidaya:", i);//System.out.println(found);
        if (found == -1) break;
        int start = found + 5;// start of actual name
        int end = string.indexOf(":", start);// System.out.println(end);
        String subString = string.substring(start, end); //System.out.println(subString);
        if(subString != null)
            count++;
        i = end + 1; //advance i to start the next iteration
    }
      System.out.println("In given String hidaya Occurred "+count+" time ");
      
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.