安装了dotnet 之后
dotnet new console -o pro_Read01
编写代码 vim pro_Read01/Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test_Read02
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string str1 = "apple orange banana";
Console.WriteLine("str1:" + str1);
string str2 = str1 + "peach";
Console.WriteLine("str2:" + str2);
if (String.Compare(str1, str2) == 0)
{ Console.WriteLine("str1,str2,same"); } else
{ Console.WriteLine("str1,str2,not same"); }
int n = str1.IndexOf(' ', 0);
Console.WriteLine("str1 first blank space in {0}", n); //查找str1中空格位置,也可用来查找单个字符
str2 = str1.Remove(n);
Console.WriteLine("remove str1 the left string is: " + str2); //删除,删除str1第一个空格后的剩余字符串
str2 = str1.Replace(' ', '-'); Console.WriteLine("replaced string is: " + str2);
str2 = str1.Insert(n, "peach"); Console.WriteLine("inserted string is: " +str2);
str2 = str1.Substring(n + 1, 6); Console.WriteLine("cutted string is : " + str2);
char chars = (' ');
string[] strs = str1.Split(chars);
Console.WriteLine("splited strings are: ");
for(int i = 0; i < strs.Length; i++) {
Console.WriteLine(i + ":" + strs[i]);
}
Console.ReadLine();
}
}
}
运行代码的方法有两种
第一种
cd pro_Read01
dotnet run
第二种, 不用cd到项目目录
dotnet run --project ./pro_Read01/pro_Read01.csproj
打印结果如下:
Hello World!
str1:apple orange banana
str2:apple orange bananapeach
str1,str2,not same
str1 first blank space in 5
remove str1 the left string is: apple
replaced string is: apple-orange-banana
inserted string is: applepeach orange banana
cutted string is : orange
splited strings are:
0:apple
1:orange
2:banana