Java:从字符串中删除数字 [英] Java: Remove numbers from string

查看:124
本文介绍了Java:从字符串中删除数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像 23.Piano+trompet 这样的字符串,我想使用这个函数从字符串中删除 23. 部分:

private String removeSignsFromName(String name) {name = name.replaceAll(" ", "");name = name.replaceAll(".", "");return name.replaceAll("\\^([0-9]+)", "");}

但它并没有这样做.此外,运行时没有错误.

解决方案

以下替换所有空白字符 (\\s)、点 (\\.),和带有 "" 的数字 (\\d):

name.replaceAll("^[\\s\\.\\d]+", "");

<块引用>

如果我想用 _ 替换 + 怎么办?

name.replaceAll("^[\\s\\.\\d]+", "").replaceAll("\\+", "_");

I have a string like 23.Piano+trompet, and i wanted to remove the 23. part from the string using this function:

private String removeSignsFromName(String name) {
    name = name.replaceAll(" ", "");
    name = name.replaceAll(".", "");

    return name.replaceAll("\\^([0-9]+)", "");
}

But it doesn't do it. Also, there is no error in runtime.

解决方案

The following replaces all whitespace characters (\\s), dots (\\.), and digits (\\d) with "":

name.replaceAll("^[\\s\\.\\d]+", "");

what if I want to replace the + with _?

name.replaceAll("^[\\s\\.\\d]+", "").replaceAll("\\+", "_");

这篇关于Java:从字符串中删除数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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