Home > Java > javaTutorial > Summary of methods for obtaining file extensions in java [regular and string interception]

Summary of methods for obtaining file extensions in java [regular and string interception]

高洛峰
Release: 2017-01-16 11:25:13
Original
1673 people have browsed it

The example in this article describes how to obtain the file extension in java. Share it with everyone for your reference, the details are as follows:

Problem description: There is a String type: String imageName = "zy.jpg"; How can I intercept the suffix name after ".".

Solution 1: Use regular expressions

package csdnTest;
import java.util.regex.*;
public class CSDNTest
{
  public static void main(String[] ss)
  {
    String s="abc.jpg";
    //String regex=".+?//.(.+)";这种写法也是可以的,但我认为没有后面的精确
    String regex=".+?//.([a-zA-z]+)";
    Pattern pt=Pattern.compile(regex);
    Matcher mt=pt.matcher(s);
    if(mt.find())
    {
      System.out.println(mt.group(1));
    }
  }
}
Copy after login

Solution 2:

System.out.println(imageName.substring(imageName.lastIndexOf('.')+1));
Copy after login

or

String FileType=imageName.substring(imageName.lastIndexOf('.')+1,imageName.length());
Copy after login

I hope this article will help everyone’s java program Design helps.

For more java method summary of obtaining file extension [regular and string interception] related articles, please pay attention to the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template