Java:从字符串中删除数值 [英] Java: removing numeric values from string

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

问题描述

我已经在这个社区的帮助下取代了从用户输入中删除数值,但是,我的代码只会在删除数字之前检索字母字符:

I have suceeded with the help of this community in removing numeric values from user input, however, my code below will only retrieve the alpha characters before the numeric that has been removed:

import java.util.Scanner;

public class Assignment2_A {

    public static void main(String[] args) {
        Scanner firstname = new Scanner(System.in);
        String firstname1 = firstname.next();
        firstname1 = firstname1.replaceAll("[^A-Z]","");
        System.out.println(firstname1);
    }
}

例如,如果用户输入= S1234am,我只是回来:S。如何检索字符串中的剩余字符?

For example if user input = S1234am, I am only getting back: S. How do I retrieve the remaining characters in the string?

推荐答案

您的正则表达式 [^ AZ] 目前仅配置为保留大写字母。您可以尝试用 [^ A-Za-z] 替换它以保留小写字母。

Your regular expression [^A-Z] is currently only configured to preserve upper-case letters. You could try replacing it with [^A-Za-z] to keep the lower-case letters too.

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

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