zoukankan      html  css  js  c++  java
  • 字符串的系列操作

    /**
    * @param regex
    * 正则表达式字符串
    * @param str
    * 要匹配的字符串
    * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
    */
    private static boolean match(String regex, String str) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(str);
    return matcher.matches();
    }
    
    // 3. 检查字符串重复出现的词
    // 
    // private void btnWord_Click(object sender, EventArgs e)
    // {
    // System.Text.RegularExpressions.MatchCollection matches =
    // System.Text.RegularExpressions.Regex.Matches(label1.Text,
    // 
    // @"(?<word>w+)s+(k<word>)",
    // System.Text.RegularExpressions.RegexOptions.Compiled |
    // System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    // if (matches.Count != 0)
    // {
    // foreach (System.Text.RegularExpressions.Match match in matches)
    // {
    // string word = match.Groups["word"].Value;
    // MessageBox.Show(word.ToString(),"英文单词");
    // }
    // }
    // else { MessageBox.Show("没有重复的单词"); }
    // 
    // 
    // }
    // 
    // 4. 替换字符串
    // 
    // private void button1_Click(object sender, EventArgs e)
    // {
    // 
    // string strResult =
    // System.Text.RegularExpressions.Regex.Replace(textBox1.Text,
    // @"[A-Za-z]*?", textBox2.Text);
    // MessageBox.Show("替换前字符:" + "
    " + textBox1.Text + "
    " + "替换的字符:" + "
    "
    // + textBox2.Text + "
    " +
    // 
    // "替换后的字符:" + "
    " + strResult,"替换");
    // 
    // }
    // 
    // 5. 拆分字符串
    // 
    // private void button1_Click(object sender, EventArgs e)
    // {
    // //实例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁
    // foreach (string s in
    // System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"d{3,4}-d*"))
    // {
    // textBox2.Text+=s; //依次输出 "甲乙丙丁"
    // }
    // 
    // }
  • 相关阅读:
    sqlhelper使用指南
    大三学长带我学习JAVA。作业1. 第1讲.Java.SE入门、JDK的下载与安装、第一个Java程序、Java程序的编译与执行 大三学长带我学习JAVA。作业1.
    pku1201 Intervals
    hdu 1364 king
    pku 3268 Silver Cow Party
    pku 3169 Layout
    hdu 2680 Choose the best route
    hdu 2983
    pku 1716 Integer Intervals
    pku 2387 Til the Cows Come Home
  • 原文地址:https://www.cnblogs.com/20gg-com/p/6037530.html
Copyright © 2011-2022 走看看