简体   繁体   English

Java:将字符串的起始字符分开

[英]Java: Separate starting character from a string

How to get Starting alphabet from the string which contain numbers and other strings?如何从包含数字和其他字符串的字符串中获取起始字母表?

Like have to separate the alphabets before the numbers starts.就像必须在数字开始之前分开字母一样。

String value1 = "test123123hj";
String value2 = "demo312342343dfs";

Output should be: Output 应该是:

value1 = "test"
value2 ="demo"

We can use the split method of string and pass in the regex to check the presence of digits.我们可以使用 string 的split方法并传入正则表达式来检查数字的存在。

"test123123hj".split("\\d+")[0]

This should give you the desired output.这应该会给你所需的 output。

在此处输入图像描述

@ChaitanyaWaikar's result is right. @ChaitanyaWaikar 的结果是正确的。 Try this.尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM