java Java从字符串中获取子字符串值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17393503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:51:43  来源:igfitidea点击:

Java get Substring value from String

javastringsplitsubstring

提问by NovusMobile

I have string

我有字符串

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

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

我只想字符串album3。我怎样才能得到那个子字符串。

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.

我正在使用substring through index.有没有其他方法,因为专辑编号正在更改,因为它会像album9, album10.

采纳答案by Brian

Obligatory regex solution using String.replaceAll:

强制性正则表达式解决方案使用String.replaceAll

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

Use of it:

它的使用:

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

回答by Ted Hopp

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

您可以使用正则表达式,但index在这种情况下使用似乎是最简单的:

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.

您可能需要进行一些错误检查,以防违反格式假设。

回答by Ruchira Gayan Ranaweera

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

回答by fge

Using Paths:

使用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:

如果您没有 Java 7,则必须求助于File

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

回答by Roman C

Use regex to match the substring

使用正则表达式匹配子字符串

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

回答by PVR

Try this ..

试试这个 ..

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

回答by Muhammad Abbas

How to count substring in String in java

如何在java中计算String中的子字符串

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

在第 8 行,我们必须使用 for 循环另一个可选的 case 替换 for 循环,使用 while 循环就像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 ");

    }

}