• C#字符串学习总结


    ---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

    字符串比较:

    eg:
    string str1 = “abcder”;
    string str2 = “gary”;
    string str3 = “gary”;
    char char1 = str1[3];
    char char2 = str2[2];
    Console.WriteLine(“{0}\n{1}”,char1,char2);//取字符串中第n个字符
    //C#中三个常见的比较字符串的方法:Compare、CompareTo、Equals方法;
    Console.WriteLine(String.Compare(str2,str1));//比较str1和str2,不想等返回-1或者1,相等返回0;
    Console.WriteLine(String.Compare(str1, str2));
    Console.WriteLine(String.Compare(str2, str3));//返回0
    Console.WriteLine(str1.CompareTo(str2));
    Console.WriteLine(String.Equals(str1, str2));//返回是否相等true或false,
    Console.ReadLine();

    格式化字符串:
    /*在C#中,string类提供了一个静态的Format方法,用于将字符串数据格式化成指定的格式,其语法格式如下:
    public static string Format(string format,object obj);
    format:用来指定字符串所要格式化的形式。
    obj:要被格式化的对象。
    返回值:format的一个副本。*/
    eg:
    DateTime dt = DateTime.Now;

    string strDate1 = String.Format(“{0: D}”, dt);
    string strDate2 = String.Format(“{0:d}”, dt);
    string strDate3 = String.Format(“{0:G}”, dt);
    Console.WriteLine(“今天的日期是:” + strDate1);//输出2011年8月25日
    Console.WriteLine(“今天的日期是:” + strDate2);//2011/8/25
    Console.WriteLine(“今天的日期是:” + strDate3);//2011/8/25 10:33:20
    Console.ReadLine();

    截取字符串:
    SubString(int startIndex,int length);//从第startIndex个字符开始截取长度为length的字符串;

    分割字符串:
    Split(params char[] separator);//将字符char分割开 如:输入为:Hello World string s = .Split(‘ ‘);//输出为world Hello

    插入和填充字符串用String类中的Insert方法和PadLeft/PadRight方法,
    插入:public string Insert(int startIndex,string value);//从第startIndex个字符插入字符串value
    填充(在字符串的开头插入字符串):public string PadLeft(int totalWidth,char paddingChar)
    //totalWidth为在字符串开头加上字符串后总的字符数,一般为str1.length+n;paddingChar为要填充的字符串
    填充(在字符串的结尾插入字符串):public string PadRight(int totalWidth,char paddingChar)
    //totalWidth为在字符串末尾加上字符串后总的字符数,一般为str1.length+n;paddingChar为要填充的字符串

    复制字符串(Copy和CopyTo方法):
    用于将字符串或者子字符串复制到另一个字符串或者char类型的数字中:
    Copy方法:public static string Copy(string str)//str为要复制的字符串。返回值:与str具有相同值的字符串
    eg:
    string str4 = “王文烽”;
    string str5;
    //使用String的Copy方法,复制字符串str4并赋值给str5
    str5 = string.Copy(str4);
    Console.WriteLine(str5);
    Console.ReadLine();
    CopyTo方法:public void CopyTo(int sourceIndex,char[]destination,int destinationIndex,int count);
    // sourceIndex:需要复制的字符的起始位置。
    // destination:目标字符串。
    // destinationIndex:指定目标数组的开始存放位置。
    // count:指定要复制的字符串个数。
    eg:
    string str = “视频学C#编程”;
    Console.WriteLine(“原字符串:” + str);
    char[] myChar = new char[3];
    //将字符串str从索引0开始的3个字符串复制到字符数组myChar中;
    str.CopyTo(0, myChar, 0, 3);
    Console.WriteLine(“复制的字符串:”);
    Console.WriteLine(myChar);
    Console.ReadLine();

    string[] lines = System.IO.File.ReadAllLines(@”1.txt”, Encoding.Default);读取txt文件(相对路径)

    static string GetConfigValue(string filename,string itemName)
    {
    string[] lines = System.IO.File.ReadAllLines(filename,Encoding.Default);
    foreach(string line in lines)//遍历
    {
    string[] strs=line.Split(‘=’);//以“=”来分割;
    string name=strs[0];
    string value=strs[1];
    if(name.Trim() == itemName)
    {
    return value.Trim;//.Trim来去掉空格;
    }
    }
    return “error”;
    }

    本文固定链接: http://www.itcodes.cn/211.html | .NET爱好者博客

    ---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ---------------------- 详细请查看:http://edu.csdn.net/heima/

  • 相关阅读:
    在SharePoint中实现Workflow(2):创建一个Workflow
    pku1384PiggyBank(动态规划)
    pku1088滑雪(记忆性搜索)
    hdu1251统计难题(初次接触字典树)
    详细解说 STL 排序(Sort)
    pku1631Bridging signals(动态规划题+二分搜索)
    pku1157LITTLE SHOP OF FLOWERS(简单动态规划题:摆放鲜花使审美价值达到最高)
    pku1067取石子游戏(博弈)
    pku2524Ubiquitous Religions(初次接触并查集)
    pku1050To the Max(求矩阵的最大子段和)
  • 原文地址:https://www.cnblogs.com/youhu/p/2776197.html
Copyright © 2020-2023  润新知