Home >Java >javaTutorial >Summary of methods for obtaining file extensions in java [regular and string interception]
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)); } } }
Solution 2:
System.out.println(imageName.substring(imageName.lastIndexOf('.')+1));
or
String FileType=imageName.substring(imageName.lastIndexOf('.')+1,imageName.length());
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!