C# 入门之字符串处理

在很多情况下,我们需要通过程序去处理一些文本,文本都是以字符串表示的,所以我们今天来看一看,使用 C# 处理字符串。

我们可以将字符串看成一个只读的 char 数组,通过数组下标的方式访问每个值:

using System;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            string testString = "Hello World!";
            char testChar = testString[1];
            Console.WriteLine($"{testChar}");
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210918/20099494vQRQI5DXMY.png

所以,我们可以将字符串转换成数组,然后通过 char 数组的方式,进行处理字符串(遍历):

using System;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            string testString = "Hello World!";
            char[] testChar = testString.ToCharArray();
            foreach ( char i in testChar)
            {
                Console.WriteLine($"{i}");
            }
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210918/2009949452ajENOQ6I.png

我们可以通过 ToUpper() 将所有字符都转换成大写:

using System;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            string testString = "Hello World!";
            string upperString = testString.ToUpper();
            Console.WriteLine($"{upperString}");
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210918/20099494L5qiRgAbWr.png

同样,你可以使用 ToLower() 将所有字符转换成小写字母;

我们可以使用 Trim() 删除字符前后的空格:

using System;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            string testString = " Hello World! ";
            Console.WriteLine($"{testString}");
            string newString = testString.Trim();
            Console.WriteLine($"{newString}");
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210918/20099494TmGo6lt2Fq.png

你也可以在 Trim() 删除指定的字符:

using System;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] trimChars = { ' ', '!' };     // 指定要删除的字符
            string testString = " Hello World! ";
            Console.WriteLine($"{testString}");
            string newString = testString.Trim(trimChars);    // 指定字符
            Console.WriteLine($"{newString}");
        }
    }
}

可以使用 PadLeft() 在字符串左边添加空格(默认空格,你也可以使用其他的字符或符号),使其达到指定的长度;

using System;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            string testString = "Hello World!";
            Console.WriteLine($"{testString}");
            string newString = testString.PadLeft(20);
            Console.WriteLine($"{newString}");
        }
    }
}

如果是在右边添加空格,可以使用 PadRight();

使用指定的字符:

using System;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            string testString = "Hello World!";
            Console.WriteLine($"{testString}");
            testString = testString.PadLeft(20,'-');
            // 字符串本身占 10 位,为了两遍一致,这里写了 30
            testString = testString.PadRight(30,'-');  
            Console.WriteLine($"{testString}");
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210918/20099494PVEbQIokp8.png

前面,我们通过 char 数组的方式,将字符串拆成一个字符一个字符处理的,但很多情况,我们需要处理的是将字符拆成一个单词一个单词,或一句话:

using System;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            string testString = "Hello World!";
            char[] separator = { ' ' };
            string[] words;
            words = testString.Split(separator);
            foreach (string word in words)
            {
                Console.WriteLine($"{word}");
            }            
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210918/200994942e2WUilAbq.png


<<:  Powershell 入门之 Alias

>>:  Spring Framework X Kotlin Day 10 Schedule

【资料结构】树的定义与追踪

树的定义 一种存资料的型态,由最初的节点延伸下多个分支,每个分支都个有个自的子分支,分支下可分割成彼...

乙级电脑软件设计技术士-Java 考照历程

在网路上很少关於这个科目的介绍,虽然第一次盲考就通过,但是整个过程还是如履薄冰。因此留下这次考试的过...

DAY 13 Big Data 5Vs – Variety(速度) Glue(1) Crawler

轻巧有弹性的Lambda能解决转档、压缩等简单的处理运算,然而在AWS上如果要建立基本完整的ETL流...

建立表与表之间的关联(Day28)

文章同时发布於: https://kevinyay945.com/smart-home-tutori...

Day12 : Docker基本操作 Dockerfile篇

应用Docker化 Docker的核心思想就是将应用给整合进Container内运行,让这个Cont...