C# 字符与字符串操作

格式化字符串:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Str1 = "hello lyshark";

            // 取出字符串中指定的字符
            char Str2 = Str1[1];

            Console.WriteLine("字符: {0} 转大写: {1} 转小写: {2}", Str2, Char.ToUpper(Str2), Char.ToLower(Str2));
            Console.WriteLine("是否为数字: {0} 是否为大写: {1} 是否为小写: {2}",
                Char.IsNumber(Str2), Char.IsUpper(Str2), Char.IsLower(Str2));

            // 将字符串转化为字符数组
            char[] chs = Str1.ToCharArray();

            for (int x = 0; x < chs.Length - 1; x++)
                Console.Write("{0} ", chs[x]);
            Console.WriteLine();

            // 将字符数组转化为字符串
            string Str3 = new string(chs);
            Console.WriteLine(Str3);

            // 格式化输出字符串
            string Str4 = "hello";
            string Str5 = "lyshark";

            string new_str = String.Format("{0},{1}", Str4, Str5);
            Console.WriteLine("格式化后的字符串: {0}", new_str);

            Console.ReadKey();
        }
    }
}

比较字符串:

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Str1 = "hello lyshark";
            string Str2 = "hello world";
            string Str3 = "hello lyshark";

            // Compare 比较字符串,相等返回0不相等返回-1
            Console.WriteLine("Str1 比较 Str2 " + String.Compare(Str1, Str2));
            Console.WriteLine("Str1 比较 Str3 " + String.Compare(Str1, Str3));

            // Compare To 比较字符串
            Console.WriteLine("Str1 比较 Str2 " + Str1.CompareTo(Str2));
            Console.WriteLine("Str1 比较 Str3 " + Str1.CompareTo(Str3));

            // Equals 比较字符串
            Console.WriteLine("Str1 比较 Str2 " + Str1.Equals(Str2));
            Console.WriteLine("Str1 比较 Str3 " + String.Equals(Str1,Str3));

            Console.ReadKey();
        }
    }
}

截取/分割字符串:

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 从第一个位置开始截取3个字符
            string Str1 = "hello lyshark";
            string Str2 = "";

            Str2 = Str1.Substring(1, 3);
            Console.WriteLine("截取数据: {0}", Str2);

            // 分割字符串变量
            string Str3 = "用^一生#下载,百度网盘,资源";
            char[] separator = { '^', '#', ',' };         // 定义分割字符

            String[] split_string = new String[100];
            split_string = Str3.Split(separator);

            for (int x = 0; x < split_string.Length;x++ )
            {
                Console.WriteLine("切割计数: {0} 切割字符串: {1}", x, split_string[x]);
            }

            // 针对时间的切割方法
            string str = "2019-12-12";
            char[] chs = { '-' };

            string[] date = str.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine("{0}年 {1}月 {2}日", date[0], date[1], date[2]);

            Console.ReadKey();
        }
    }
}

插入/删除字符串:

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 插入字符串的演示
            string Str1 = "下载";
            Str1 = Str1.Insert(0, "用一生时间");
            Console.WriteLine(Str1);

            string Str2;
            Str2 = Str1.Insert(Str1.Length, "百度网盘里面的资源");
            Console.WriteLine(Str2);

            // 填充字符串的演示
            string Str3;
            Str3 = Str1.PadLeft(Str1.Length + 3, '*');    // 在左侧填充
            Console.WriteLine("左侧填充: " + Str3);
            Str3 = Str1.PadRight(Str1.Length + 3, '*');   // 在右侧填充
            Console.WriteLine("右侧填充: " + Str3);

            // 去空格的实现
            string str = "            hahahah          ";
            str = str.Trim();
            str = str.TrimStart();
            str = str.TrimEnd();

            // 删除字符串的演示
            Console.WriteLine("从索引3处向后删除: " + Str3.Remove(3));
            Console.WriteLine("删除指定个数的字符: " + Str3.Remove(1, 3));

            Console.ReadKey();
        }
    }
}

拷贝替换字符串:

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 普通的拷贝字符串
            string Str1 = "hello lyshark";
            string Str2;
            Str2 = string.Copy(Str1);
            Console.WriteLine("普通拷贝: " + Str2);

            // 替换字符串
            string Str3 = "one world,one dream";
            string Str4 = Str3.Replace(',','*');
            Console.WriteLine("将,替换为** => " + Str4);

            string Str5 = Str3.Replace("one", "One");
            Console.WriteLine("将one替换为One =>" + Str5);

            Console.ReadKey();
        }
    }
}

寻找开头结尾字符串:

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "今天天气好晴朗,处处好风光";

            // 寻找字符串开头结尾
            if (str.StartsWith("今") && str.EndsWith("光"))
                Console.WriteLine("ok");

            // 从指定字符开始搜索,并返回位置
            int index = str.IndexOf("天气", 0);
            Console.WriteLine(index);

            // 从结束位置开始搜索
            string path = @"c:ac苍de苍fg\fdfdfdfdvfd苍老师.wav";
            int path_index = path.LastIndexOf("\");
            Console.WriteLine(path_index);

            Console.ReadKey();
        }
    }
}

串联字符串:

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Join 将指定字符串使用|串联起来
            string name_str = string.Join("|", "张三", "李四", "王五", "赵六", "田七");
            Console.WriteLine(name_str);

            // 将字符串切割后串联去掉竖线
            String[] u_name = { "张三", "李四" ,"王五"};
            string ptr = string.Join("|", u_name);
            Console.WriteLine("合并后: " + ptr);

            string[] strNew = ptr.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine("去掉| = " + strNew[1]);

            for (int x = 0; x < strNew.Length;x++ )
                Console.WriteLine("去掉竖线 [{0}] = {1}",x,strNew[x]);
            
            Console.ReadKey();
        }
    }
}

字符串倒序输出:

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "hello lyshark";
            
            // 实现反转字符串 hello -> olleh
            char[] chs = str.ToCharArray();

            for (int x = 0; x < chs.Length / 2;x++ )
            {
                char tmp = chs[x];
                chs[x] = chs[chs.Length - 1 - x];
                chs[chs.Length - 1 - x] = tmp;
            }
            str = new string(chs);
            Console.WriteLine("反转后的结果: {0}", str);

            // 实现反转单词 hello lyshark -> lyshark hello
            string str1 = "hello lyshark";

            string[] strNew = str1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            for (int x = 0; x < strNew.Length / 2; x++)
            {
                string tmp = strNew[x];
                strNew[x] = strNew[strNew.Length - 1 - x];
                strNew[strNew.Length - 1 - x] = tmp;
            }
            str1 = string.Join(" ", strNew);
            Console.WriteLine("反转后的结果: {0}", str1);

            Console.ReadKey();
        }
    }
}

IndexOf搜索字符串:

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 通过indexOf 切割特定字符
            string email = "admin@blib.cn";
            int index = email.IndexOf("@");

            string userName = email.Substring(0, index);
            string userHost = email.Substring(index + 1);

            Console.WriteLine("名字: {0} 主机: {1}",userName,userHost);

            // 寻找指定字符出现位置
            string str = "abcd wwabcd asdcdsac waascd ascsaaa";
            int index1 = str.IndexOf('a');
            int count = 1;

            while(index1 != -1)
            {
                count++;
                index1 = str.IndexOf('a', index1 + 1);
                if (index1 == -1)
                    break;

                Console.WriteLine("第{0}次出现a的位置是{1}", count, index1);
            }

            Console.ReadKey();
        }
    }
}

版权声明: 本博客,文章与代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章【均为原创】作品,转载请务必【添加出处】,您添加出处是我创作的动力!

警告:如果您恶意转载本人文章,则您的整站文章,将会变为我的原创作品,请相互尊重!
原文地址:https://www.cnblogs.com/LyShark/p/13156497.html