字符串: 是一系列Unicode字符,一个字符数组
char char ='1';
string str = "abc"; //一系列Unicode字符,一个字符数组
相当于: char[] str = new char[]{'a','b','c'};
另一种 初始化字符串
string str = new string(new char[] { 'a','b','c'});
字符串的基本方法:
string str = " abc ";
str.TrimStart()
str.TrimEnd()
str.Trim()
char.IsDigit(char c)
char.IsDigit(string s,int index)
char.IsWhiteSpace(char c)
char.IsWhiteSpace(string s,int index)
str.Replace(old,new)
str.Substring(int startIndex)
str.Substring(int startIndex, int Length)
str.Contains(string/char) //是否包含
str.IndexOf(string/char) //第一次出现的位置,找不到为 -1
str.ToUpper();
str.ToLower();
str.Split('|');
str.Split(new char[] { '*', '|', '#' });
str.Remove(int startIndex)
str.Remove(int startIndex,int Count);
str.Insert(int startIndex,string value)
....
练习: 身份证题目
Int32.Parse(string) // 将字符串解析为 Int32类型
string k = "12";
int n = 10;
int r = n + Int32.Parse(k);
Console.WriteLine(r);
随机发生器 System.Random
Random rdm = new Random();
Random rdm = new Random(int Seed);
//只要种子一样,运行产生的也一定一样。
//所以可以放入如时间,秒等伪种子,别再循环体内声明
Random rdm = new Random();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(rdm.Next(1,10));
}
练习: 随机发扑克牌给4个人
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Class2
{
public static void Main(string[] args)
{
ArrayList list = InitialCards();
ArrayList[] player = new ArrayList[4];
player[0] = new ArrayList();
player[1] = new ArrayList();
player[2] = new ArrayList();
player[3] = new ArrayList();
Random rdm = new Random();
string card = null;
int index = 0;
while (list.Count > 0)
{
index = rdm.Next(0, list.Count);
card = list[index].ToString();
player[(54-list.Count)%4].Add(card);
list.RemoveAt(index);
}
DisplayReslut(player);
}
private static void DisplayReslut(ArrayList[] player)
{
for (int i = 0; i < player.Length; i++)
{
Console.WriteLine("player[{0}]", i);
int k = 0;
foreach (string card2 in player[i])
{
if (k % 5 == 0)
{
Console.WriteLine();
}
++k;
Console.Write("{0}/t", card2);
}
Console.WriteLine();
}
}
public static ArrayList InitialCards()
{
string[] color = new string[] { "红桃", "黑桃", "方块", "梅花" };
string[] point = new string[] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
ArrayList list = new ArrayList(54);
for (int i = 0; i < color.Length; i++)
{
for (int j = 0; j < point.Length; j++)
{
list.Add(color[i] + "" + point[j]);
}
}
list.Add("大王");
list.Add("小王");
return list;
}
}
}