如何检查字符串是否包含Java中的数字格式? [英] How to check that the string contains the number format in Java?

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

问题描述

我是applet概念的新手。我想实现一个应用程序。小程序有2个文本区域和一个按钮。第一个文本区域是输入(意味着它包含一些文本)。第二个文本区域为空。如果单击该按钮,则解析第一个文本区域中的文本(此处解析表示第一个文本区域文本包含一些文本对吗?该文本包含一些数字,数字将被删除,剩余文本将被粘贴在第二个文本区域)
EX:
文本区域1:1。堆栈溢出2谷歌3雅虎
按钮后点击
文本区域2:堆栈溢出
Google
Yahoo

I am new to applet concept. I want to implement the one application. The applet have 2 text areas and one button. 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

这里如何检查字符串中的数字?以及如何获取字符串。

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);
        }
    }

}


推荐答案

只需使用:

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

它用一个替换所有数字字符和点换行符,所以如果你把结果放到JTextArea,每个名字都在一行上。

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.

要获得数字之间的字符串,请使用split()方法。

To get the Strings between the numbers, use the split() method.

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

正则表达式现在还删除了数字和名称之间的空格。 String [] names 将包含单个名称。

The regex now also removes whitespaces between the numbers and the names. String[] names will contain the single names.

这篇关于如何检查字符串是否包含Java中的数字格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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