如何获取字符串中第一个字母的位置 [英] How to get position of the first letter in a string
本文介绍了如何获取字符串中第一个字母的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获取字符串中第一个字母的位置.
I need to get position of the first letter in a string.
遵循一个基本示例.第一个字母是T
,所以我需要知道T
的位置,为此我可以使用indexOf("T")
.
Follow a basic example. The first letter is T
, So I need to know the position of T
, for that I can use indexOf("T")
.
我想通过正则表达式获取第一个字母,而不是添加硬编码的字母 (T
).
I would like to get the first letter by regex instead to add the letter hard-coded (T
).
提前致谢.
public class RegexMatches {
public static void main(String args[]) {
String line = "000 1 This is my message";
int first = line.indexOf("T");
line = line.substring(first, line.length());
System.out.println(line);
}
}
推荐答案
如果我阅读了 Java 文档 正确,您正在寻找匹配对象的 start
方法:
If I read the Java docs correctly, you're looking for the start
method of the match object:
String line = "000 1 This is my message";
Pattern p = Pattern.compile("\\p{L}");
Matcher m = p.matcher(line);
if (m.find()) {
System.out.println(m.start());
}
这篇关于如何获取字符串中第一个字母的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文