当前位置:编程学习 > JAVA >>

java 字符串处理

我想在文本域里输入一串字符串,比如:string str = "qwee,dfddfg;erertrt,fgdfgfds;";
字符串的规则是:值1,值2;
中间是个逗号,末尾是个分号;

请教各位大神,该如何实现呢? 最好是有java或者JS代码实现哦。

谢啦; java string js --------------------编程问答-------------------- 你要怎么样啊,你的问题都还没有提出来 --------------------编程问答--------------------
引用 1 楼 wufengui1315 的回复:
你要怎么样啊,你的问题都还没有提出来


我需要的是判断输入字符串是否合法。

字符串的规则是:值1,值2;
中间是个逗号,末尾是个分号; --------------------编程问答-------------------- 只好自己顶下了 --------------------编程问答-------------------- Copy from 《Swing Hacks》



Hack 49. Force Text Input into Specific Formats

 

Use Java's powerful pattern matching to enforce rules on typed input

Validating input is an important GUI task, and some applications will validate your input when you tab off a field or even validate it on every keystroke. After all, it's a lot easier to deal with bogus data by not letting it into your system in the first place.

One technique for validating user input is to use a regular expression and then evaluate the input against it. For example, a field that can be uppercase letters only must always match the expression [A-Z]*, and one that can be any combination of uppercase, lowercase, numbers, and spaces must match [A-Za-z0-9]* (notice the space after 9).

Java's regex feature lets you create TextComponents that enforce matching against an expression. The basic idea is to watch for changes in the underlying Document and do your pattern match then.

7.3.1. Constraining a Document

Hopefully, you won't be surprised to know that you don't need to touch the view classesJTextField, JTextArea, etc.to add text constraint functionality. Text entry is happening in the modelin other words the Documentso that's where you tie in your regex code. This hack, listed in Example 7-2, subclasses PlainDocument to run the regex check on every call to insertString().

Example 7-2. A document allowing input that matches only a regex

 import javax.swing.text.*; 
import java.util.regex.*;

public class RegexConstrainedDocument extends PlainDocument {

Pattern pattern;
Matcher matcher;

public RegexConstrainedDocument () { super(); } 
public RegexConstrainedDocument (AbstractDocument.Content c) { super(c); }
public RegexConstrainedDocument (AbstractDocument.Content c, String p) {
super (c);
setPatternByString (p);
}
public RegexConstrainedDocument (String p) {
super();
setPatternByString (p);
}

public void setPatternByString (String p) {
Pattern pattern = Pattern.compile (p);
// checks the document against the new pattern
// and removes the content if it no longer matches
try {
matcher = pattern.matcher (getText(0, getLength())); 
System.out.println ("matcher reset to " + getText (0, getLength()));
if (! matcher.matches()) {
System.out.println ("does not match");
remove (0, getLength());
}
} catch (BadLocationException ble) {
ble.printStackTrace(); // impossible?
}
}

public Pattern getPattern() { return pattern; }

public void insertString (int offs, String s, AttributeSet a)
throws BadLocationException {
// consider whether this insert will match
String proposedInsert =
getText (0, offs) +
s +
getText (offs, getLength() - offs);
System.out.println ("proposing to change to: " +
proposedInsert);
        if (matcher != null) {
matcher.reset (proposedInsert);
System.out.println ("matcher reset");
if (! matcher.matches()) {
System.out.println ("insert doesn't match");
return;
}
}
super.insertString (offs, s, a);

}

This class holds onto a Pattern and a Matcher to perform the regex matching. The pattern can be set in the constructor, or later with a call to setPatternByString(). In either case, the method compiles the pattern and creates a Matcher with the Document's text. Changing the pattern could, of course, create a mismatch with any existing text in the Document, so the Matcher immediately calls matches() and if the text does not match, it deletes all the text from the Document.

Perhaps the more typical case is when the Document.insertString() method is called. This will happen on every keystroke in a JTextComponent. Assuming the Matcher is not null, meaning that the Pattern has been set at some point, you simply need to call Matcher.matches() against the Document's new contents to see if they comply with the regex constraints. If not, return early, never calling the superclass's insert() method, and thus disallowing the input.

7.3.2. Adding Constrained Text Fields

Since the Document isn't actually visible, you need to put it in something in order to test it. The TestRegexConstrainedDocument class creates a JTextField for you to type in a regular expression to enforce, and a longer JTextField for text to test. You create this latter JTextField with the seldom-seen constructor that takes a Document, an initial-value String, and a width (specified in columns). Obviously, you use a RegexConstrainedDocument for the first argument. There's also a Set JButton that takes the String value of the regex field and sets that as the new pattern to match the document against, with RegexConstrainedDocument's setPatternByString() method. The rest is mostly layout code. The test class is shown in Example 7-3.

Example 7-3. GUI to test the RegexConstrainedDocument in a JTextField

 import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*;

public class TestRegexConstrainedDocument extends JPanel 
implements ActionListener {

JTextField regexField, filterField;
JButton regexButton;
RegexConstrainedDocument regexDoc;

public TestRegexConstrainedDocument() {
  setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
  // top - regex stuff
  JPanel topPanel = new JPanel();
  JLabel rLabel = new JLabel ("regex:" );
  topPanel.add (rLabel);
  regexField = new JTextField (20);
  topPanel.add(regexField);
  regexButton = new JButton ("Set");
  regexButton.addActionListener (this);
  topPanel.add (regexButton);
  add (topPanel);
  // bottom - filterfield
  regexDoc =
new RegexConstrainedDocument ();
  filterField = new JTextField (regexDoc, "", 50);
  add (filterField);
   }

   public void actionPerformed (ActionEvent e) {
System.out.println ("actionperformed");
if (e.getSource() == regexButton) {
System.out.println ("regexbutton");
regexDoc.setPatternByString (regexField.getText());
}
}

public static void main (String[] args) {
JComponent c = new TestRegexConstrainedDocument();
JFrame f = new JFrame ("Regex filtering");
f.getContentPane().add (c);
f.pack();
f.setVisible(true);

}

When run, the displayed GUI looks like Figure 7-2. Notice that by using the regex [A-Z ]*, you can enter uppercase letters and spaces, but nothing else.

When you type a comma, a question mark, or lowercase letters, they are ignored.

Figure 7-2. Filtering JTextField input with a regular expression

 

You can do smart things with this tool, but also really stupid things. Let's say you want to restrict input to the characters that will be in a North American phone number of the form 123-456-7890. You can set the pattern to [0-9\-]*, which will allow the user to type in only numbers and the hyphen.

Now you're feeling clever, but you don't like the fact that this still allows users to type patterns that aren't phone numbers, like 1111111 or -1-1-1-1. So, you set the pattern to an exact description of the phone number pattern:

[0-9]{3}-[0-9]{3}-[0-9]{4}
But now your users can't enter anything! Why? Because although that regular expression does specify the phone number pattern, no substring of it will ever match. This expression specifies exactly 12 characters, so when you type the first one, there's only one characterit doesn't match the pattern, so it's rejected.

The moral of the story here is to be thoughtful. You might decide to wire up a FocusListener so you impose the regex pattern only when the user moves off the field, and change the "delete everything" behavior to something a little less forceful; for example, you could pop up a dialog telling the user that her input isn't in the right format. Just don't be surprised when regular expressions give you results that are logical, but sometimes unexpected.
--------------------编程问答-------------------- --------------------编程问答--------------------  public static boolean getStr(String str)
    {
        if(str.indexOf(",")>0 && !str.endsWith(",") && str.endsWith(";"))
        {
            return true;
        }
        return false;
    }
     --------------------编程问答-------------------- 你写正则表达式去实现吧。 --------------------编程问答-------------------- public class HelloWorld{
public static void main(String[] args){
if(isLegal("qwee,dfddfg;erertrt,fgdfgfds;"))
System.out.println("符合条件");
else
System.out.println("不符合");
}
static boolean isLegal(String str){
if (!str.endsWith(";"))
return false;
String[] s = str.split(";");
if(s.length < 2)
return false;
for(int i = 0;i< s.length - 1;i++){
String[] s2 = s[i].split(",");
if(s2.length == 2){
continue;
}
else{
return false;
}
}
return true;
}
}
不知道是否理解错了楼主的意思。是否要判断的都是:值1,值2的形式
楼主试试看吧 --------------------编程问答--------------------
引用 6 楼 wangyangtfr 的回复:
 public static boolean getStr(String str)
    {
        if(str.indexOf(",")>0 && !str.endsWith(",") && str.endsWith(";"))
        {
            return true;
        }
        return false;
    }
    
这个可以    --------------------编程问答--------------------
引用 9 楼 xiaofeifei123 的回复:
Quote: 引用 6 楼 wangyangtfr 的回复:

 public static boolean getStr(String str)
    {
        if(str.indexOf(",")>0 && !str.endsWith(",") && str.endsWith(";"))
        {
            return true;
        }
        return false;
    }
    
这个可以   


你这个不能处理中文的;号 --------------------编程问答--------------------
引用 7 楼 licip 的回复:
你写正则表达式去实现吧。


正有此意,不过不太会写。求正则表达式解决方案 --------------------编程问答--------------------
引用 5 楼 Inhibitory 的回复:


这么长的文章,是要表达啥意? --------------------编程问答-------------------- 正则表达式是个好的解决方案。 --------------------编程问答--------------------
引用 8 楼 zjwben 的回复:
public class HelloWorld{
public static void main(String[] args){
if(isLegal("qwee,dfddfg;erertrt,fgdfgfds;"))
System.out.println("符合条件");
else
System.out.println("不符合");
}
static boolean isLegal(String str){
if (!str.endsWith(";"))
return false;
String[] s = str.split(";");
if(s.length < 2)
return false;
for(int i = 0;i< s.length - 1;i++){
String[] s2 = s[i].split(",");
if(s2.length == 2){
continue;
}
else{
return false;
}
}
return true;
}
}
不知道是否理解错了楼主的意思。是否要判断的都是:值1,值2的形式
楼主试试看吧


值1,值2的意思是以分号结尾,也有可能是值1,值2;值3,值4;以此内推,中间只能有一个逗号。你这段程序随便在中间多加一个逗号,运行结果就会不对的。 --------------------编程问答--------------------
引用 10 楼 u010215566 的回复:
Quote: 引用 9 楼 xiaofeifei123 的回复:

Quote: 引用 6 楼 wangyangtfr 的回复:

 public static boolean getStr(String str)
    {
        if(str.indexOf(",")>0 && !str.endsWith(",") && str.endsWith(";"))
        {
            return true;
        }
        return false;
    }
    
这个可以   


你这个不能处理中文的;号

你可以单独针对中文的符号 处理一下  思想是一样的 --------------------编程问答--------------------
引用 15 楼 xiaofeifei123 的回复:
Quote: 引用 10 楼 u010215566 的回复:

Quote: 引用 9 楼 xiaofeifei123 的回复:

Quote: 引用 6 楼 wangyangtfr 的回复:

 public static boolean getStr(String str)
    {
        if(str.indexOf(",")>0 && !str.endsWith(",") && str.endsWith(";"))
        {
            return true;
        }
        return false;
    }
    
这个可以   


你这个不能处理中文的;号

你可以单独针对中文的符号 处理一下  思想是一样的


你这种思想我早解决了的 --------------------编程问答--------------------
引用 14 楼 u010215566 的回复:
Quote: 引用 8 楼 zjwben 的回复:

public class HelloWorld{
public static void main(String[] args){
if(isLegal("qwee,dfddfg;erertrt,fgdfgfds;"))
System.out.println("符合条件");
else
System.out.println("不符合");
}
static boolean isLegal(String str){
if (!str.endsWith(";"))
return false;
String[] s = str.split(";");
if(s.length < 2)
return false;
for(int i = 0;i< s.length - 1;i++){
String[] s2 = s[i].split(",");
if(s2.length == 2){
continue;
}
else{
return false;
}
}
return true;
}
}
不知道是否理解错了楼主的意思。是否要判断的都是:值1,值2的形式
楼主试试看吧


值1,值2的意思是以分号结尾,也有可能是值1,值2;值3,值4;以此内推,中间只能有一个逗号。你这段程序随便在中间多加一个逗号,运行结果就会不对的。

楼主是我这个不算中文字符的话是可以的吧。。。楼上那个好像不行吧。。我还特意打开eclipse试了试。。。 --------------------编程问答-------------------- 你这个也有问题呀,一个分号内存在多个逗号就会出现问题的 --------------------编程问答-------------------- 只好自己顶下了 --------------------编程问答-------------------- 字符串的处理还是比较可以的
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,