如何获得文件扩展名

来源:互联网 发布:华为工作 知乎 编辑:程序博客网 时间:2024/04/29 10:04
<script type="text/javascript"><!--google_ad_client = "pub-1654710028568330";/* 728x90, 创建于 08-5-19 */google_ad_slot = "5790075209";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><script type="text/javascript"><!--google_ad_client = "pub-1654710028568330";/* 728x15, 创建于 08-5-19 */google_ad_slot = "1395130186";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 

 

 

问题描述:  有一个String类型:String imageName = "zy.jpg"; 请问我如何截取"."后面的后辍名.

解法一:我的解法,使用正则表达式

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));}}}
解法二:
System.out.println(imageName.substring(imageName.lastIndexOf('.')+1));

 

或者

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

 

 

 

 

 

 

<script type="text/javascript"><!--google_ad_client = "pub-1654710028568330";/* 728x90, 创建于 08-5-19 */google_ad_slot = "5790075209";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><script type="text/javascript"><!--google_ad_client = "pub-1654710028568330";/* 728x15, 创建于 08-5-19 */google_ad_slot = "1395130186";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击