可以使用正则表达式来截取不带汉字的字符串。以下是一个示例代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String str = "km/h 千米每小时";
        String regex = "[^\\x00-\\xff]+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            String result = str.substring(0, matcher.start());
            System.out.println(result);
        }
    }
}

在上面的代码中,使用正则表达式[^\\x00-\\xff]+来匹配不带汉字的部分。然后使用substring方法截取字符串的子串。最后输出结果为km/h

kmh 千米每小时 用java语言截取不带汉字的字符串

原文地址: https://www.cveoy.top/t/topic/izNr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录