用点作为分隔符分割字符串 [英] Split string with dot as delimiter

查看:36
本文介绍了用点作为分隔符分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否打算在 . 上拆分字符串是正确的方法吗?我的代码是:

I am wondering if I am going about splitting a string on a . the right way? My code is:

String[] fn = filename.split(".");
return fn[0];

我只需要字符串的第一部分,这就是我返回第一项的原因.我问是因为我在 API 中注意到 . 表示任何字符,所以现在我卡住了.

I only need the first part of the string, that's why I return the first item. I ask because I noticed in the API that . means any character, so now I'm stuck.

推荐答案

split() 接受一个正则表达式,所以你需要转义 . 以不把它当作正则表达式元字符.这是一个例子:

split() accepts a regular expression, so you need to escape . to not consider it as a regex meta character. Here's an example :

String[] fn = filename.split("\."); 
return fn[0];

这篇关于用点作为分隔符分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆