码迷,mamicode.com
首页 > Windows程序 > 详细

9. C#-- 字符串的一些操作

时间:2015-05-12 19:04:46      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{

    class Program
{
        static void Main(string[] args)
{
           //string类型变量 可以看作只读数组
            string myString = "A String";
            char myChar=myString[0];
            Console.WriteLine("output a char");
            Console.WriteLine("{0}", myChar);
            Console.WriteLine("output string");
            foreach(char character in myString)
{
               //Console.WriteLine(myChar);
                Console.WriteLine("{0}",character);
}
           //对数组进行处理(删除e,s,输入y时,做特殊处理,并输出结果)
            Console.WriteLine("delete char ‘e‘,‘s‘, make a change when click ‘y‘;");
            char []trimChars = {‘ ‘,‘e‘,‘s‘};
            string userResponse = Console.ReadLine();
userResponse = userResponse.ToLower();
userResponse = userResponse.Trim(trimChars);
            if (userResponse == "y")
{
                Console.WriteLine("you click the y");
}
            Console.WriteLine(userResponse);
            //对数组进行处理,删除前后空格
            Console.WriteLine("delete the space in the beginning and end;");
            string userResponse2 = Console.ReadLine();
userResponse2=userResponse2.TrimStart();
userResponse2 = userResponse2.TrimEnd();
            Console.WriteLine(userResponse2);
            //对数组进行处理,添加前后空格
            Console.WriteLine("add the space in the beginning;");
            string userResponse3 = Console.ReadLine();
userResponse3 = userResponse3.PadLeft(21);
            Console.WriteLine(userResponse3);
            //对数组进行处理, 对字符串左边添加‘-‘,使之达到21个字符
            //userResponse3 = userResponse3.PadLeft(21,‘-‘);
            //对数组进行处理, 对字符串右边添加空格,使之达到21个字符
            //userResponse3 = userResponse3.PadRight(21);
            //对数组进行处理,  根据空格划分,输出每个字符串;
            Console.WriteLine("output every string ,split by space;");
            char[] separator = { ‘ ‘ };
            string myString3 = "this is a";
            string[] myWords;
myWords = myString3.Split(separator);
            foreach(string word in myWords)
{
                Console.WriteLine(word);
}
            //对数组进行处理, 转换大小写,输出字符串长度
            string myString4="this is a";
myString4=myString4.ToUpper();
            Console.WriteLine(myString4);
myString4=myString4.ToLower();
            Console.WriteLine(myString4);
            int i= myString4.Length;
            Console.WriteLine(i);
            Console.ReadLine();
 
}
}
}


本文出自 “Ricky's Blog” 博客,请务必保留此出处http://57388.blog.51cto.com/47388/1650695

9. C#-- 字符串的一些操作

标签:c#

原文地址:http://57388.blog.51cto.com/47388/1650695

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!