C#字符串操作

/*
假设有一字符串strS="This Is An Apple.",使用字符串方法,完成下面的要求。
(1)取出字符串strS中的第9个字符,然后统计该字符串strS中出现的次数。
(2)统计字符串中单词的个数。
(3)将字符串反序并全部转换为大写字符输出。
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cx
{
    class Program
    {
        static void Main(string[] args)
        {
            string strS = "This Is An Apple.";
            if (!string.IsNullOrEmpty(strS))     //判断是否为空
            {
                char findChar = Convert.ToChar(strS.Substring(8, 1));  //从字符串strS的startIndex开始取length个字符
                int count = GetCharCount(strS, findChar);
                string[] word = strS.Split(' ');           //将字符串按空格分离成二维数组
                int len = word.Length;
                string strSReverse1 = MyReverse1(strS).ToUpper();   //将其转换成大写
                Console.WriteLine("\"This Is An Apple.\"共有{0}个单词,{1}出现了{2}次", len, findChar, count);
                Console.WriteLine(strSReverse1);
                string strSReverse2 = MyReverse2(strS).ToUpper();   //将其转换成大写
                Console.WriteLine("\"This Is An Apple.\"共有{0}个单词,{1}出现了{2}次", len, findChar, count);
                Console.WriteLine(strSReverse2);
            }
            else
                Console.WriteLine("字符串为空");
            Console.ReadKey();
        }
        public static int GetCharCount(string strS, char findChar)   //计算字符findChar的个数
        {
            int count = 0;
            char[] c = strS.ToCharArray();
            for (int i = c.Length - 1; i >= 0; i--)
            {
                if (c[i] == findChar)
                    count++;
            }
            return count;
        }
        public static string MyReverse1(string strS)    //将字符串strs反序
        {
            string strreverse = "";
            char[] c = strS.ToCharArray();
            for (int i = c.Length - 1; i >= 0; i--)
                strreverse += c[i].ToString();
            return strreverse;
        }

        public static string MyReverse2(string strS)    //将字符串strS反序   
        {
            StringBuilder strReverse = new StringBuilder();
            char[] c = strS.ToCharArray();
            for (int i = c.Length - 1; i >= 0; i--)   
            {
                strReverse.Append(c[i]);
            }
            return strReverse.ToString();
        }
    }
}


运行结果:


 

/*
实验StringBuilder类。定义一个静态成员方法,该方法实现字符串反转。
如:Reverse("6221982")返回值为2891226。
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cx
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("数组反序后为:{0}",Reverse("6221982"));
            Console.ReadKey();
        }


        // 代码出现超限错误
        public static string Reverse(string strs)    //将字符串strs反序
        {
            StringBuilder strreverse = new StringBuilder();
            char[] c = strs.ToCharArray();
            for (int i = c.Length - 1; i >= 0; i--)
            {
                strreverse.Append(c[i]);
            }
            return strreverse.ToString();
        }
    }
}


运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值