简体   繁体   English

如何检查字符串是否包含Java中的数字格式?

[英]How to check that the string contains the number format in Java?

I am new to applet concept. 我是applet概念的新手。 I want to implement the one application. 我要实现一个应用程序。 The applet have 2 text areas and one button. 该小程序具有2个文本区域和一个按钮。 The first text area is the input(means it contains some text).Second Text area is empty.If you click on the button then the text in the first text area is parsed(here parsed means first text area text contains some text right? that text contains some number here that numbers will be removed and remaining text will be pasted in the second text area) EX: Text area1: 1. stack Overflow 2 Google 3 yahoo after button click Text area2: stack Overflow Google Yahoo 第一个文本区域是输入(意味着它包含一些文本)。第二个文本区域是空的。如果单击按钮,则第一个文本区域中的文本将被解析(这里的解析意味着第一个文本区域的文本包含一些文本,对吗?该文本包含一些数字,数字将被删除,剩余文本将粘贴在第二个文本区域中)EX:文本区域1:1.堆栈溢出2 Google 3雅虎,单击按钮后文本区域2:堆栈溢出Google Yahoo

Here how to check the number in the string? 这里如何检查字符串中的数字? and how to get the string up to the number. 以及如何使字符串达到数字。

public class parsetextdata extends Applet implements ActionListener{

    TextArea ta1,ta2;
    Button parse;
    public void init() { 
        ta1 = new TextArea();

        ta1.setText("1. Naresh Repalle 2. Lakshman Yalavarthy 3. Rajendra Batchu 4. Bhart Chand Yalavrthy ");
        add(ta1); 

        parse = new Button();
        parse.setLabel("parse");
        add(parse); 

        ta2 = new TextArea();    
        add(ta2);              

        parse.addActionListener(this);

      }

    @Override
    public void actionPerformed(ActionEvent button) {

        if(button.getSource() == parse)
        {
            String text = ta1.getText();        
            ta2.setText(text);
        }
    }

}

Just use: 只需使用:

text.replaceAll("[\\\\w]*[0-9.]+[\\\\w]*", "\\n");

It replaces all numeric characters and dots with a newline character, so if you put the result to a JTextArea, every name will be on a single line. 它将所有数字字符和点替换为换行符,因此,如果将结果放入JTextArea,则每个名称都将在一行上。

To get the Strings between the numbers, use the split() method. 要获取数字之间的字符串,请使用split()方法。

String[] names = text.split("[\\w]*[0-9.]+[\\w]*");

The regex now also removes whitespaces between the numbers and the names. 正则表达式现在还删除了数字和名称之间的空格。 String[] names will contain the single names. String[] names将包含单个名称。

如果您的目标只是删除数字(以及示例中的空白),则可以像下面这样简单地使用String.replaceAll:

ta2.setText(text.replaceAll("[0-9]+ ?", ""));

You can use following code 您可以使用以下代码

private static ArrayList <Character> getNumberRemoved(String string) {

    ArrayList <Character> list = new ArrayList <Character> ();
    char [] inString = string.toCharArray();
    for (char ch : inString)
    {
        if (ch >= '0' && ch <='9')
            continue;
        list.add(ch);
    }

    return list;
}

This will take O(n) time. 这将花费O(n)时间。 basically walk through the characters and skip numbers. 基本上遍历字符并跳过数字。

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

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