c# 字符串常用函数练习

本文通过一个C#程序示例,展示了如何使用多种方法来处理字符串,包括连接、分割、填充、转换大小写等基本操作,并介绍了如何进行子串提取、替换及判断字符串是否为空。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharp
{
    
public class TestString
    {
        
public TestString()
        {
            
string[] sA = new string[5] { "H""e""l""l""o" };
            Console.WriteLine(String.Concat(sA));
            Console.WriteLine(String.Join(
"-",sA));

            
string s = String.Join("", sA);
            Console.WriteLine(s);

            
char[] c = s.ToCharArray();

            
string sl = s.ToLower();
            
string su = s.ToUpper();

            Console.WriteLine(sl 
+ "--" + su);
            
string s2 = s.PadLeft(10'_');
            Console.WriteLine(s2);

            
string s3 = s.PadRight(10'_');
            Console.WriteLine(s3);

            
string s4 = String.Join("-", sA);
            
string[] s5 = s4.Split(new char[]{'-'});
            
foreach (string s6 in s5)
            {
                Console.WriteLine(s6);
            }

            
string s7 = s2.Trim();
            Console.WriteLine(s7);
            
string s8 = s2.Trim(new char[] { '_' });
            Console.WriteLine(s8);

            
//TrimEnd,TrimStart 同理.

            
if (s2.StartsWith("H"))
            {
                Console.WriteLine(
"S2 was start with 'H'");
            }
            
else
            {
                Console.WriteLine(
"S2 wasn't start with 'H'");
            }

            
if (s2.EndsWith("o"))
            {
                Console.WriteLine(
"S2 was end with 'o'");
            }
            
else
            {
                Console.WriteLine(
"S2 wasn't end with 'o'");
            }
            
            Console.WriteLine(s2.Substring(
5,s2.Length-5));
            Console.WriteLine(s2.Remove(
05));

            Console.WriteLine(s2.Replace(
"l","i"));

            
if (String.IsNullOrEmpty(s2))
            {
                Console.WriteLine(
"S2 Is NullOrEmpty");
            }
            
else
            {
                Console.WriteLine(
"S2 Isn't NullOrEmpty");
            }

        }
    }
}

转载于:https://www.cnblogs.com/nevernet/archive/2009/01/12/1374463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值