字符串处理:字符串处理技术

  介绍说明: 这些串处理小窍门技巧在平时编程中会普遍遇到, 整理总结了很多, 这里只提供些比较好玩供大家参考.

  实现基本思路方法都是C#提供有关串处理常用思路方法, 此处不再介绍说明.

  . 根据标点符号分行某

  输入: abc,defg,hijklmn,opq,rstuv (测试时按逗号分行, 可自定义分行符)

  输出: abc

  defg

  hijklmn

  opq

  rstuv

Code
      oldstr = textBox1.Text.Trim;
      str = oldstr.Split(',');
      for ( i = 0; i < str.Length; i)
      {
         (richTextBox1.Text "")
          richTextBox1.Text = str[i].;
        
          richTextBox1.Text "n" + str[i].;
      }


  2. 将串颠倒输出

  输入: ABCDEFG

  输出: GFEDCBA

Code
       str1 = textBox1.Text.Trim;
      char charstr = str1.ToCharArray;
      Array.Reverse(charstr);
       str2 = (charstr);
      textBox2.Text = str2;


  3. 巧截数字

  输入: A23BCDEFG4Hi678

  输出: 234678

Code
CharEnumerator CEnumerator = textBox1.Text.GetEnumerator;
      while (CEnumerator.MoveNext)
      {
         .gif' /> = [1];
        .gif' /> = .Text.Encoding.ASCII.GetBytes(CEnumerator.Current.);
         asciicode = ()(.gif' />[0]);
         (asciicode >= 48 && asciicode <= 57)
        {
          textBox2.Text CEnumerator.Current.;
        }
      }


  4. 找出串中某所有位置

  输入: aBcdaEFGaHIaaaK, 查找: a

  输出: 0,4,8,11,12,13

Code
str = textBox1.Text.Trim;
      char myChar = str.ToCharArray;
      for ( i = 0; i < myChar.Length; i)
      {
         (myChar[i]. textBox2.Text.Trim)
          MessageBox.Show("串" + textBox2.Text.Trim + "在" + textBox1.Text.Trim + "中位置为:" + i. + "n");
      }


  5.从串分离文件路经, 文件名及扩展名

  输入: C:gdiplus.dll

  输出:  路径: C

  文件名: gdiplus

  扩展名:dll

Code
strPath = textBox1.Text.Sub(0, textBox1.Text.LastIndexOf(""));
       strName=textBox1.Text.Sub(textBox1.Text.LastIndexOf("")+1,(textBox1.Text.LastIndexOf(".")-textBox1.Text.LastIndexOf("")-1) );
       strEName = textBox1.Text.Sub(textBox1.Text.LastIndexOf(".")+1, (textBox1.Text.Length - textBox1.Text.LastIndexOf(".")-1));
      MessageBox.Show("文件路径:"+strPath +"n 文件名:"+strName +"n 文件扩展名:"+strEName ,"信息",MessageBoxButtons.OK,MessageBoxIcon.Information );


  6.批量替换某

  输入: abcdsfjlsdkfjalsdkabcdefadslkfjlksdafabc

  查找: abc

  替换: ***

  输出: ***dsfjlsdkfjalsdk***defadslkfjlksdaf***

Code
     public M__index = -1;
     private M__start;
     private M__end;
      M__index = 0;
      while (M__index != -1)
      {
        M__start = 0;
        M__end = richTextBox1.Text.Trim.Length;
        M__index = richTextBox1.Find(this.textBox1.Text.Trim, M__start, M__end, RichTextBoxFinds.None);
         (M__index -1)
        {
          MessageBox.Show(this, "全部'" + this.textBox1.Text + "'已替换完毕", "未找到",
            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        
        {
          richTextBox1.SelectedText = textBox2.Text;
          M__index this.textBox1.Text.Length;
        }
      }


  7.把个按空格分割串存储在中 (此处测试用ArrayList)

  输入: abc def ghiklm opq

  输出: 可按下标输出: 如 arr[1]=def

Code
       str = "abc def ghiklm opq";
       strArr = str.Split(' ');
      .Collections.ArrayList mylist = .Collections.ArrayList;
      foreach ( strArray in strArr)
      {
        mylist.Add(strArray);
      }
      listBox1.DataSource = mylist;


  8.对串进行加密

  输入: abc

  输出: cvJ5W08AdsA=

Code
      textBox1.ReadOnly = false;
      try
      {
        DESCryptoServiceProvider descsp = DESCryptoServiceProvider;
         key = Encoding.Unicode.GetBytes(encryptKey);
         data = Encoding.Unicode.GetBytes(textBox1.Text.Trim);
        MemoryStream MStream = MemoryStream;
        CryptoStream CStream = CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
        CStream.Write(data, 0, data.Length);
        CStream.FlushFinalBlock;
        textBox2.Text = Convert.ToBase64String(MStream.ToArray);
        textBox3.Text = "";
        textBox3.ReadOnly = true;
      }
      catch(Exception ex)
      {
        MessageBox.Show(ex.Message, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
      }


  9.对串进行解密 (和上面例子配合使用)

  输入: cvJ5W08AdsA=

  输出: abc

Code
      textBox3.ReadOnly = false;
      try
      {
        DESCryptoServiceProvider descsp = DESCryptoServiceProvider;
         key = Encoding.Unicode.GetBytes(encryptKey);
         data = Convert.FromBase64String(textBox2.Text.Trim);
        MemoryStream MStream = MemoryStream;
        CryptoStream CStream = CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
        CStream.Write(data, 0, data.Length);
        CStream.FlushFinalBlock;
        textBox3.Text = Encoding.Unicode.GetString(MStream.ToArray);
        textBox1.Text = "";
        textBox1.ReadOnly = true;
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
      }




  10.区别 0, 空串, Null, Empty和 Nothing

  (1).对于声明后未赋值数值类型变量它们默认值为0;

  (2).对于声明后未赋值串变量则缺省值为空串"";

  (3).Null关键字介绍说明变量不包含有效数据它是将Null值显式地赋值给变量结果也可能是包含Null表达式的间进行运算结果

  (4).Empty关键字表示未变量缺省值

  (5).Nothing关键字用于将对象变量从实际对象中分离开来

  补充介绍说明: 些常用串处理技术如首字母转化为大写 , 串比较, 添加子串等操作比较简单, 此处略

  文章来源: http://www.cnblogs.com/ziyiFly/archive/2008/09/17/1292488.html



Tags:  vb字符串处理 js字符串处理 字符串处理函数 字符串处理

延伸阅读

最新评论

发表评论