using System;
using System.IO;
using System.Text;
namespace _058_字符串的练习
{
class Program
{
static void Main(string[] args)
{
//1、"abcdefg"->"gfedcba"
string s = "abcdefg";
char[] chs = s.ToCharArray();
for (int i = 0; i < chs.Length / 2; i++)
{
char temp = chs[i];
chs[i] = chs[chs.Length - i - 1];
chs[chs.Length - i - 1] = temp;
}
s = new string(chs);
Console.WriteLine(s);
//2、"Hello C Sharp"->"Sharp C Hellp"
string s1 = "Hello C Sharp";
string[] str = s1.Split(" ", StringSplitOptions.RemoveEmptyEntries);
for (int i = 2; i >=0 ; i--)
{
Console.Write(str[i]+" ");
}
Console.WriteLine();
//s1 = string.Join(" ", s1);
//3、从Email中提取用户名和域名:abc@163.com
string email = "abc@163.com";
string[] str1 = email.Split("@", StringSplitOptions.RemoveEmptyEntries);
string userName = str1[0];
string domainName = str1[1];
//int index = email.IndexOf('@');
//string userName = email.Substring(0, index);
//string domainName = email.Substring(index+1);
Console.WriteLine("用户名:{0}\n域名:{1}",userName,domainName);
//4、读取文本
string path = @"E:\cSharptext1.txt";
string[] contents = File.ReadAllLines(path, Encoding.Default);
for (int i = 0; i < contents.Length; i++)
{
string[] strNew = contents[i].Split(" ", StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine((strNew[0].Length>7?strNew[0].Substring(0,7)+"...":strNew[0]) + '|' + strNew[1]);
}
//5、用户输入数据,找出所有“ ”的位置
string userData = Console.ReadLine();
//int Count1 = 0;
//for (int i = 0; i < userData.Length; i++)
//{
// if (userData[i]=='我')
// {
// Count1++;
// Console.WriteLine("第{0}次出现的位置是{1}", Count1, i);
// }
//}
int index = userData.IndexOf('我');
int count = 1;//记录出现次数
Console.WriteLine("第{0}次出现的位置是{1}", count, index);
//循环体:从上一次出现' '的位置+1的位置找下一次出现' '的位置
//循环条件:index!=-1
while (index!=-1)
{
count++;
index = userData.IndexOf('我', index + 1);
if (index == -1)
{
break;
}
Console.WriteLine("第{0}次出现的位置是{1}",count,index);
}
//6、"老牛很邪恶"->"老牛很**"
string s3 = "老牛很邪恶";
if (s3.Contains("邪恶"))
{
s3 = s3.Replace("邪恶", "**");
}
Console.WriteLine(s3);
Console.ReadKey();
}
}
}