数组的遍历和字符串的操作

数组的遍历

遍历数组有三种循环方式

  • for循环
int i = 0;
int[] scores = { 12, 16, 2, 6, 7, 23, 7, 4, 7 };
Console.WriteLine("for循环遍历数组");
for(i = 0; i < scores.Length; i++)
{
    Console.WriteLine(scores[i]);
}
  • while循环
while (i < scores.Length)
            {
                Console.WriteLine(scores[i]);
                i++;
            }
  • foreach循环
foreach (int temp in scores)
            {
                Console.WriteLine(temp);
            }

三种循环方法,只有foreach循环遍历数组时,不能够对数组里的元素进行任何的操作。

字符串的操作

字符串实质上可以看做是一个字符数组,所以字符串也可以遍历并且输出

string str = "  www.baidu.COM  ";

for(int i = 0; i < str.Length; i++)
{
    Console.Write(str[i] + " ");
}

修改字符串的方法有很多,下面是示例代码

string res1 = str.ToUpper(); //将字符串转换成大写
string res2 = str.ToLower(); //将字符串转换成小写
string res3 = str.Trim(); //删除字符串前后端的空白
string res4 = str.TrimStart(); //删除字符串前面的空白
string res5 = str.TrimEnd(); //删除字符串后面的空白
string[] res6 = str.Split('.'); //将字符串以指定的字符分开

最后的输出结果如下图
输出结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值