Nov
04

http://msdn.microsoft.com/zh-/library/ms228599.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

//=============修改字符串内容( 编程指南)==========
/**/
 //=========使用System.Text.RegularExpressions.Regex 类======

//class ReplaceSubstrings
//{
//    string searchFor;
//    string replaceWith;
//        // Custom match method called by Regex.Replace
//    string ReplaceMatchCase(Match m)
//    {
//        // Test whether the match is capitalized
//        if (Char.IsUpper(m.Value[0]) == true)
//        {
//            // Capitalize the replacement string
//            // using System.Text;
//            StringBuilder sb = new StringBuilder(replaceWith);
//            sb[0] = (Char.ToUpper(sb[0]));//将替换字符的首字母大写
//            return sb.ToString();
//        }
//        else
//        {
//            return replaceWith;
//        }
//    }
//    static void Main(string[] args)
//    {

//        ReplaceSubstrings RS = new ReplaceSubstrings();
//        string s = "The mountains are behind the clouds today.";
       
//        // Replace one substring with another with String.Replace.
//        // Only exact matches are supported.
//        s = s.Replace("mountains", "peaks");
//        Console.WriteLine(s);
//        // Output: The peaks are behind the clouds today.

//        // Use Regex.Replace for more flexibility.
//        // Replace "the" or "The" with "many" or "Many".
//        // using System.Text.RegularExpressions
//        RS.searchFor = "the"; // A very simple regular expression.
//        RS.replaceWith = "many";
//        s = Regex.Replace(s, RS.searchFor, RS.ReplaceMatchCase, RegexOptions.IgnoreCase);
//        Console.WriteLine(s);
//        // Output: Many peaks are behind many clouds today.

//    //    // Replace all occurrences of one char with another.
//    //    s = s.Replace(' ', '_');
//    //    Console.WriteLine(s);
//    //    // Output: Many_peaks_are_behind_many_clouds_today.

//    //    // Remove a substring from the middle of the string.
//    //    string temp = "many_";
//    //    int i = s.IndexOf(temp);
//    //    if (i >= 0)
//    //    {
//    //        s = s.Remove(i, temp.Length);
//    //    }
//    //    Console.WriteLine(s);
//    //    // Output: Many_peaks_are_behind_clouds_today.

//    //    // Remove trailing and leading whitespace.
//    //    // See also the TrimStart and TrimEnd methods.
//    //    string s2 = "    I'm wider than I need to be.      ";
//    //    // Store the results in a new string variable.
//    //    temp = s2.Trim();
//    //    Console.WriteLine(temp);
//    //    // Output: I'm wider than I need to be.

//    //    // Keep the console window open in debug mode.
//    //    Console.WriteLine("Press any key to exit");
//    //    Console.ReadKey();
//    }
//}

//2.=============使用ToCharArray方法===================
//class stringModify2
//{
//    static void Main(String[] args)
//    {
//        string s1 = "he is a Chinese";
//        Console.WriteLine(s1);
//        char[] s2 = s1.ToCharArray();//将string转化成字符数组
//        for (int i = 0; i < s2.Length; i++)
//        {
//            Console.WriteLine(s2[i]);
//        }
//        int index = s1.IndexOf('C');//获得'C'在原串是s1中的索引值
//        s2[index] = 'c';//将小写c转换为大写
//        Console.WriteLine("test s2 "+s2);//输出 (test s2 System.Char[]).说明此时s2只是个数组
//        Console.WriteLine(s2);//强制转换为字符串
//        s2.ToString();//将数组转化为字符串
//        Console.WriteLine("此时s2为字符串"+s2);
//    }
//}
//3.============使用StringBuiler类提供的可“就地”修改的字符缓冲区===
//class stringModify3
//{
//    static void Main()
//    {
//        string s1 = "he is a Chinese";
//        Console.WriteLine(s1);
//        StringBuilder s2 = new StringBuilder(s1);
//        for (int i = 0; i < s2.Length; i++)
//        {
//            Console.WriteLine(s2[i]);
//        }
//        //StringBuilder类不提供IndexOf方法
//        char c = 'C';
//        int index=getIndex(s2,c);
//        Console.WriteLine("index={0}",index);
//        s2[index] = 'c';
//        Console.WriteLine(s2);
//    }
//    static int getIndex(StringBuilder s2, char c)//获取'C'在原字符串中的索引值
//    {
//        int index = 0;
//        while (index < s2.Length)
//        {
//            if (s2[index] == 'C')
//            {
//                return index;
//            }
//            else
//                index++;

//        }
//        return 0;
//    }
//}

//4.==========不安全代码,使用 fixed 关键字“就地”访问各个字符=============
//不仅要在Main前加上unsafe,同时编译时要在后跟上参数 /unsafe
class stringModify4
{
    unsafe static void Main()
    {
        string s1 = "he is a Chinese";
        string s2 = s1;//s1,s2指向同一个对象
        fixed (char* p = s1)
        {
            p[8] = 'c';
        }
        Console.WriteLine(s1);
        Console.WriteLine(s2);//s2的内容同时也被修改
        //由于这种方法是直接在对象字符串上操作,所以当其中一个引用修改时,其他的引用也被修改了
    }
}

标签:,


随机文章:

最近阅读过此文章的网友:

Tags:

没人甩我?!

到此一游

注意: 你可以使用如下标签代码: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

| || |
由yo2提供博客服务 |Theme created by In My Pad.