查找字符串中的第一个字母
Find first letter in a string
我的程序使用的 ID 系统要求字母前有 1-3 位数字,字符后有 1-3 位数字。例如:12a123
或1b83
等
我想知道的是如何找到字符串中第一次出现的字母,以便我可以存储该字母,因为它用于稍后数字之间的运算。
谢谢:)
您可以使用正则表达式完成此任务,例如:
System.out.println("12a123".replaceAll("\d{1,3}([A-z])\d{1,3}", ""));
细分:
\d{1,3}
匹配一个数字(等于 [0-9]
)
{1,3}
量词——匹配 1 到 3 次,尽可能多,按需回馈(贪心)
- 第 1 个捕获组 (
[A-z]
)
- 匹配下面列表中的单个字符
[A-z]
A-z A(索引 65)和 z(索引 122)之间范围内的单个字符(区分大小写)
我可以提出两个解决方案:
方案 1
如果您使用的是 Java 8,您可以使用 :
String str = "12a123";
char firstCharacter = (char) str.chars()
.filter(c -> String.valueOf((char) c).matches("[a-zA-Z]"))
.findFirst()
.orElseThrow(() -> new Exception("No character exist"));//a
方案 2
您可以像这样使用 replaceAll :
String str = "12a123";
String firstCharacter = str.replaceAll("\d{1,3}([A-z])\d{1,3}", "");//a
只需遍历字符并获取 upper/lowercase A-Z 范围内的第一个字符。
public char getCharacter(final String code)
{
for (char character : code.toCharArray())
{
if ( (character >= 'a' && character <= 'z')
|| (character >= 'A' && character <= 'Z'))
{
return character;
}
}
throw new RuntimeException("No character in ID: " + code);
}
使用 Java 的字符 class 的 isLetter 方法。
看起来像;
public class CharacterTest {
private static Character getFirstCharInString(final String candid)
{
int found = 0;
char [] candids = candid.toCharArray();
for(found = 0; found < candids.length; found++)
{
if(Character.isLetter(candids[found])) break;
}
return new Character(candids[found]);
}
public static void main(String[] args) {
String ids = "12a123";
String ids2 = "1b83";
System.out.println(getFirstCharInString(ids));
System.out.println(getFirstCharInString(ids2));
}
}
这个和 Aniket 的路线相似。
public class ExampleCode
{
public static void main(String[] args)
{
String[] input = { "12a234", "1s324" };
String[] operations = new String[input.length];
for (int i=0; i < input.length; i++)
{
operations[i] = token.replaceAll("\d{1,3}([A-z])\d{1,3}", ""));
}
}
}
我的程序使用的 ID 系统要求字母前有 1-3 位数字,字符后有 1-3 位数字。例如:12a123
或1b83
等
我想知道的是如何找到字符串中第一次出现的字母,以便我可以存储该字母,因为它用于稍后数字之间的运算。
谢谢:)
您可以使用正则表达式完成此任务,例如:
System.out.println("12a123".replaceAll("\d{1,3}([A-z])\d{1,3}", ""));
细分:
\d{1,3}
匹配一个数字(等于[0-9]
){1,3}
量词——匹配 1 到 3 次,尽可能多,按需回馈(贪心)- 第 1 个捕获组 (
[A-z]
)- 匹配下面列表中的单个字符
[A-z]
A-z A(索引 65)和 z(索引 122)之间范围内的单个字符(区分大小写)
- 匹配下面列表中的单个字符
我可以提出两个解决方案:
方案 1
如果您使用的是 Java 8,您可以使用 :
String str = "12a123";
char firstCharacter = (char) str.chars()
.filter(c -> String.valueOf((char) c).matches("[a-zA-Z]"))
.findFirst()
.orElseThrow(() -> new Exception("No character exist"));//a
方案 2
您可以像这样使用 replaceAll :
String str = "12a123";
String firstCharacter = str.replaceAll("\d{1,3}([A-z])\d{1,3}", "");//a
只需遍历字符并获取 upper/lowercase A-Z 范围内的第一个字符。
public char getCharacter(final String code)
{
for (char character : code.toCharArray())
{
if ( (character >= 'a' && character <= 'z')
|| (character >= 'A' && character <= 'Z'))
{
return character;
}
}
throw new RuntimeException("No character in ID: " + code);
}
使用 Java 的字符 class 的 isLetter 方法。
看起来像;
public class CharacterTest {
private static Character getFirstCharInString(final String candid)
{
int found = 0;
char [] candids = candid.toCharArray();
for(found = 0; found < candids.length; found++)
{
if(Character.isLetter(candids[found])) break;
}
return new Character(candids[found]);
}
public static void main(String[] args) {
String ids = "12a123";
String ids2 = "1b83";
System.out.println(getFirstCharInString(ids));
System.out.println(getFirstCharInString(ids2));
}
}
这个和 Aniket 的路线相似。
public class ExampleCode
{
public static void main(String[] args)
{
String[] input = { "12a234", "1s324" };
String[] operations = new String[input.length];
for (int i=0; i < input.length; i++)
{
operations[i] = token.replaceAll("\d{1,3}([A-z])\d{1,3}", ""));
}
}
}