c# split方法
1.对单个字符进行分解 分割的是字符,不是字符串,只能用单引号 split('xxxx')
string s = string.Empty;
string str = "aascaacbbcssc";
string[] arr = str.Split('c');
foreach (var i in arr)
{
s += i + " ";
}
// s:aas aa bb ss
2.对多个字符进行分割
string s = string.Empty;
string str = "abcdeabcdeabcde";
string[] arr = str.Split( new char[3] { 'c', 'd', 'e' });
foreach (var i in arr)
{
s += i + " ";
}
//s:ab ab ab
3.正则表达式进行分割
string s = string.Empty;
string str = "aaajsbbbjsccc";
string[] brr = Regex.Split(str, "js", RegexOptions.IgnoreCase);
foreach (var i in brr)
{
s += i + " ";
}
//s:aaa bbb ccc
split重载方法
方法调用
方法二: String.Split(Char[],Int32)
string str = "aaatbbscctdd";
string []strArray = str.Split(new char[]{'t',2});
//只切割成2份(result :"aaa" "bbscctdd")
方法三:String.Split(Char[],StringSplitOptions)
string str = "aaatbbscctddt";
string []strArray = str.Split(new char[]{'t',StringSplitOptions.RemoveEmptyEntries});
//去除空元素(result :"aaa" "bbscc" "dd")
string []strArray = str.Split(new char[]{'t',StringSplitOptions.None});
//保留空元素(result :"aaa" "bbscc" "dd" “”)
方法四:String.Split(String[],StringSplitOptions)
string str = "aaatbbscctddt";
string []strArray = str.Split(new String[]{"t",StringSplitOptions.RemoveEmptyEntries});
//去除空元素(result :"aaa" "bbscc" "dd")
string []strArray = str.Split(new String[]{"t",StringSplitOptions.None});
//保留空元素(result :"aaa" "bbscc" "dd" “”)
方法五:String.Split(Char[],Int32,StringSplitOptions)
string str = "aaatbbscctddt";
string []strArray = str.Split(new char[]{'t',2,StringSplitOptions.RemoveEmptyEntries});
//切割成2份且去除空元素(result :"aaa" "bbscctddt")
string []strArray = str.Split(new char[]{'t',2,StringSplitOptions.None});
//切割成2份且保留空元素(result :"aaa" "bbscctddt" “”)
方法六:.String.Split(String[],Int32,StringSplitOptions)
string str = "aaatbbscctddt";
string []strArray = str.Split(new String[]{"t",2,StringSplitOptions.RemoveEmptyEntries});
//切割成2份且去除空元素(result :"aaa" "bbscctddt")
string []strArray = str.Split(new String[]{"t",2,StringSplitOptions.None});//
切割成2份且保留空元素(result :"aaa" "bbscctddt" “”)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _车票系统
{
class Program
{
static void Main(string[] args)
{
Console.Title = "火车票管理系统";
string[,] zuo = new string[3, 4];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
zuo[i, j] = (i+1)+"_"+(j+1)+"有票 ";
}
}
string s = string.Empty;
while (true)
{
System.Console.Clear();
Console.WriteLine("\n 火车票管理系统 " + "\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
System.Console.Write(zuo[i, j]);
}
System.Console.WriteLine();
}
System.Console.Write("请输入座位行号和列号(如:0,2)输入q退出:");
s = System.Console.ReadLine();
if (s == "q") break;
string[] ss = s.Split(',');
int one = int.Parse(ss[0]);
int two = int.Parse(ss[1]);
one -= 1;
two -= 1;
if (one <= 3 && two <= 4)
{
if (zuo[one, two] != (one+1) + "_" + (two+1) + "无票 ")
{
zuo[one, two] = (one+1)+"_"+(two+1)+"无票 ";
System.Console.Write("购票成功,按回车键继续购票!");
s = System.Console.ReadLine();
}
else
{
System.Console.Write("购票失败,请按回车返回重新购票!");
s = System.Console.ReadLine();
}
}
else
{
System.Console.Write("购票失败,请按回车返回重新购票!");
s = System.Console.ReadLine();
}
}
}
}
}
output:
---
火车票管理系统
1_1有票 1_2有票 1_3有票 1_4有票
2_1有票 2_2有票 2_3有票 2_4有票
3_1有票 3_2有票 3_3有票 3_4有票
请输入座位行号和列号(如:0,2)输入q退出:1,3
购票成功,按回车键继续购票!
火车票管理系统
1_1有票 1_2有票 1_3无票 1_4有票
2_1有票 2_2有票 2_3有票 2_4有票
3_1有票 3_2有票 3_3有票 3_4有票
请输入座位行号和列号(如:0,2)输入q退出:2,3
购票成功,按回车键继续购票!
火车票管理系统
1_1有票 1_2有票 1_3无票 1_4有票
2_1有票 2_2有票 2_3无票 2_4有票
3_1有票 3_2有票 3_3有票 3_4有票
请输入座位行号和列号(如:0,2)输入q退出:q
C:\Users\Administrator\source\repos\ConsoleApp2\bin\Debug\netcoreapp3.1\ConsoleApp2.exe (进程 10736)已退出,代码为 0。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace T1_2
{
class Program
{
static void PrintArray2d(char[,] a2d)
{
for(int i = 0;i< a2d.GetLength(0); i++)
{
for(int j = 0;j < a2d.GetLength(1); j++)
{
Console.Write(a2d[i, j]);
}
}
}
static void Main(string[] args)
{
string s = ""
+ "#################\n"
+ "# #\n"
+ "# ~~~ H #\n"
+ "# ~~~ #\n"
+ "# ~~~ #\n"
+ "# C ~~~ #\n"
+ "# D C ~~~ #\n"
+ "# #\n"
+ "# #\n"
+ "#################\n";
Console.Write(s);
char c = s[0]; // c == '#'
char[,] map = new char[10, 18];
for (int i = 0; i < map.GetLength(0); i++)
{
for(int j = 0;j < map.GetLength(1); j++)
{
map[i, j] = s[i * 18 + j];
}
}
map[1, 1] = 'o';
int oi = 1;
int oj = 1;
while (true)
{
Console.Clear();
PrintArray2d(map);
char x = char.Parse(Console.ReadLine());
switch (x)
{
case 'w':
{
if (oi > 1)
{
map[oi, oj] = ' ';
oi--;
map[oi, oj] = 'o';
}
break;
}
case 's':
{
if (oi <8)
{
map[oi, oj] = ' ';
oi++;
map[oi, oj] = 'o';
}
break;
}
case 'a':
{
if (oj > 1)
{
map[oi, oj] = ' ';
oj--;
map[oi, oj] = 'o';
}
break;
}
case 'd':
{
if (oi < 10)
{
map[oi, oj] = ' ';
oj++;
map[oi, oj] = 'o';
}
break;
}
}
}
}
}
}