using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ex1.String
{
class Program
{
static void Main(string[] args)
{
string str1, str2;
str1 = Console.ReadLine();
str2 = Console.ReadLine();
Console.WriteLine("{0} \n{1}", str1, str2);
str1 = str2;
Console.WriteLine("{0} \n{1}", str1, str2);
//Substring来提取子串
string str3;
str3 = str1.Substring(4);
Console.WriteLine(str3);
str3 = str1.Substring(4,4);
Console.WriteLine(str3);
//比较字符串大小
str1 = "my";
str2 = "you";
int i = string.Compare(str1, str2);
int j = str1.CompareTo(str2);
if (i == 0)
Console.WriteLine("str1 = str2");
if (i > 0)
Console.WriteLine("str1 > str2");
if (i < 0)
Console.WriteLine("str1 < str2");
//replace() 替换
string str4;
str4 = Console.ReadLine();
str4 = str4.Replace("a", "A");
Console.WriteLine(str4);
//拆分split() 用字符进行分隔 后得到多个拆分的字符串 所以要用string[]数组来存储
string str = "How. are you:what's. you: name:.I love java";
//用单个字符 或者多个字符进行分隔
char split1 = ':';
char[] split = new char[]{'.',':'};
string[] sss;
string[] sss1;
sss = str.Split(split);
sss1 = str.Split(split1);
foreach (string a in sss)
{
Console.Write(a+" ");
}
Console.WriteLine();
foreach (string a in sss1)
{
Console.Write(a + " ");
}
Console.WriteLine();
//也可以使用字符串数组来进行分隔
string[] split2 = {":."};
string[] sss2;
sss2 = str.Split(split2,StringSplitOptions.RemoveEmptyEntries);
foreach (string a in sss2)
{
Console.Write(a + " ");
}
}
}
}
String简单使用
最新推荐文章于 2022-12-01 19:40:02 发布