C#小知识整理二

C#小知识整理二

1.新数组接收原数组不为0的数。

//①求出原数组不为0的长度

int[] num = { 10, 20, 30, 0, 40, 5, 10, 0 };

int count = 0;

for(int i=0;i<num.Length;i++)

{

if(num[i]!=0)

{

count++;

}

}

int[] num2 = new int[count];

//②原数组中不为0的赋值给新数组,同时新数组的下标自加

原数组的下标不是新数组的下标

int t = 0;

for (int i = 0; i < num.Length; i++)

{

if (num[i] != 0)

{

num2[t] = num[i];

t++;

}

}

//③遍历输出新数组的数

for (int i = 0; i < num2.Length; i++)

Console.WriteLine(num2[i]);

Console.ReadKey();

2.{"gao", "zhang", "wang", "tao"} 输出 "gao"|"zhang"|"wang"|"tao"

string[] strs = { "gao", "zhang", "wang", "tao" };

string s = "";

for (int i = 0; i < strs.Length-1; i++)

{

s += strs[i]+"|";

}

s += strs[strs.Length-1]

Console.Write(s); Console.ReadKey();

3.foreach遍历数组

string[] strs = { "gao", "zhang", "wang", "tao" };

//在数组中依次遍历 in strs(数组名) 保存到变量s中

foreach (string s in strs)

Console.WriteLine(s);

执行当s等于"wang"时跳出本次循环,执行下一次循环。

string[] strs = { "gao", "zhang", "wang", "tao" };

foreach (string s in strs)

{

if (s == "wang")

{

continue;

}

Console.WriteLine(s);

}

输出"gao", "zhang", "tao"

4.

static void Main(string[] args)

{

Num();

}

static void Num()

{

Console.WriteLine("请输入一个整数");

string s= Console.ReadLine();

Console.WriteLine(s);

Console.ReadKey()

}

5.输入字符串,输出次数。

static void Main(string[] args)

{

Console.WriteLine("请输入您的字符串");

string s = Console.ReadLine();

Console.WriteLine("请输入您的次数");

int i=Convert.ToInt32(Console.ReadLine());

string f = Strs(i, s);

Console.WriteLine(f);

Console.ReadKey();

}

static string Strs(int n,string s)

{

string nums = "";

for (int i = 0; i <n; i++)

{

nums+=s;

}

return nums;

}

6.函数返回值必须有值

static void Main(string[] args)

{

int i1 = 40;

int i2= 20;

int i3 = Num(i1, i2);

Console.WriteLine(i3);

Console.ReadKey();

}

static int Num(int a, int b)

{

if (a > b)

{

return 30;

}

else if (a == b)

{

return 20;

}

else //如果没有此语句,那么可能无法返回值

{ return 0; }

}

7.传数组,分隔符()到函数里进行函数的构造。

static void Main(string[] args)

{

string[] strs = { "aa", "bb", "cc", "dd" };

string sf = Str(strs, "()");

Console.WriteLine(sf);

Console.ReadKey();

}

static string Str(string[] s, string fg)

{

string st = "";

for (int i = 0; i < s.Length; i++)

st=st+ s[i] + fg;

return st;

}

8.函数的重载

只有参数的类型、顺序不一致才能函数重名,函数返回值类型一致与否没关系

构成重载的条件:参数类型不同或者参数个数不同(不严谨的),与返回值无关。

static void Main(string[] args)

{

SayHellow(123);

Console.ReadKey();

}

static void SayHellow(string a)

{

Console.WriteLine("您好啊!!!");

}

static void SayHellow(int n)

{

Console.WriteLine(102);

}

9.可变参数params 可变参数应当放到最后,即最后一个参数是可变参数

个数不确定的可变参数以数组的形式传递

int[] num = { 10, 20, 30, 45 };

//可以①int t = Bnum(num);②int t = Bnum(10, 20, 30, 45);

int t = Bnum(10, 20, 30, 45);

Console.WriteLine(t);

Console.ReadKey();

static int Bnum(params int[] n)

{

int max = 0;

for (int i = 0; i < n.Length; i++)

{

if (n[i] > max)

max = n[i];

}

return max;

}

10.ref 与out

ref需要赋值进行传递

out得出调用函数(外部函数)里的值,本身的不需要赋值

(1)折腾的是复制品,内部不影响外部 10 20

static void Main(string[] args)

{

int i = 10;

int s = 20;

Doint(i, s);

Console.WriteLine("{0},{1}",i,s);

Console.ReadKey();

}

static void Doint(int i, int s)

{

int temp = 0;

temp = i;

i = s;

s = temp;

}

(2)使用了ref 使用之前必须给变量赋值,内部影响外部 20 10

static void Main(string[] args)

{

int i = 10;

int s = 20;

Doint(ref i, ref s);

Console.WriteLine("{0},{1}",i,s);

Console.ReadKey();

}

static void Doint(ref int i, ref int s)

{

int temp = 0;

temp = i;

i = s;

s = temp;

}

(3)//Parse(Console.ReadLine(), out b) 测试类型转换是否成功

static void Main(string[] args)

{

bool b= false;//不需要为out赋值

int i = Parse(Console.ReadLine(), out b);

if (b)

{

Console.WriteLine("成功,i={0}", i);

}

else

{

Console.WriteLine("错误");

}

Console.ReadKey();

}

static int Parse(string s,out bool success)

{

//Console.WriteLine(success);

if(s=="一")

{

success = true;

return 1;

}

else if(s=="二")

{

success = true;

return 2;

}

else

{

success = false;

return -1;

}

}

(4)判断string 与int 之间类型转换是否成功

string s = Console.ReadLine();

int i;

if(int.TryParse(s,out i))

{

Console .WriteLine ("转换成功");

}

else

{

Console .WriteLine ("转换失败");

}

11.字符串函数(一)

(1)测试长度 " " 长度为1 null无法测试到长度

string s = " ";

string m = null;

Console.WriteLine(s.Length);

Console.ReadKey();

(2)string.IsNullOrEmpty(s)是否是"" 或null

null的长度无法确定,Empty的长度为0," " 空格的长度为1

string s = "";

if (string.IsNullOrEmpty(s))

{

Console.WriteLine("长度为0");

}

else

{

Console.WriteLine("有长度了");

}

(3)无法对指定的字符进行修改,只能进行访问或者生成一个新的字符串。

(4)可以生成一个新的字符串

string s = "abcdefg";

char[] chr=s.ToCharArray ();//①成为char的数组

chr[2] = 'k'; //②对指定索引的字符进行更改

string s1 = new string(chr);//③生成一个新的字符串

Console.WriteLine(s1);

Console.WriteLine(s);

Console.ReadKey();

(5)对数值型数组的更改

int[] num = { 10, 20, 30, 40 };

num[1] = num[1] + 100;

foreach (int i in num)

Console.WriteLine(i);

Console.ReadKey();

(6)2009年9月5日 改写成 二零零九年九月五日

//利用函数构造

static void Main(string[] args)

{

string s =Console .ReadLine();

string f = Strs(s);

Console.WriteLine(f);

Console.ReadKey();

}

static string Strs(string a)

{

char[] ymd = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };

char[] chr = a.ToCharArray();

int j = 0;

for (int i = 0; i < chr.Length; i++)

{

if (chr[i] > 48 && chr[i] < 58) //判断当前的字符是否在0~9之间 2009年9月1日 chr[0]—2

{

j = chr[i] - 48; //求出该字符对应的下标索引 chr[0]也就是2的ASCii的值 减去48得出下标

chr[i] = ymd[j]; //新的数组的下标的值赋值给当前数组 chr[0]对应的新的数组是ymd[2] 二 }

string s1 = new string(chr);

} return s1;

}

(2)第二种方法

string s =Console .ReadLine();

string f = Strs(s);

Console.WriteLine(f);

Console.ReadKey();

static string Strs(string a)

{

char[] ymd = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };

char[] chr = a.ToCharArray();

int j = 0;

for (int i = 0; i < chr.Length; i++)

{

if (chr[i] > 48 && chr[i] < 58) //判断当前的字符是否在0~9之间 2009年9月1日 chr[0]—2

{

j = chr[i] - 48; //求出该字符对应的下标索引 chr[0]也就是2的ASCii的值 减去48得出下标 50-48=2

chr[i] = ymd[j]; //新的数组的下标的值赋值给当前数组 chr[0]对应的新的数组是ymd[2] 二

}

}

string s1 = new string(chr);

return s1;

}

12.字符串函数(二)

字符串大小写比较 范围(相同范围进行对比),大小写(不区分大小写) 顺序(一样)

bool b = "abc".Equals("AbC", StringComparison.OrdinalIgnoreCase);

Console .Write (b);

字符串劈开//s.Split('|')

(1)string s = "aa|bb|c"

string[] f = s.Split('|'); //是'' 而不是" " ★分开的是数组,而不是字符串

foreach (string e in f)

Console.Write(e);

(2)//s.Split (new char[]{','},StringSplitOptions .RemoveEmptyEntries )

//RemoveEmptyEntries移除空的字符串数组元素 none 保留空的字符串数组元素。

string s = "aa,bb,cc,,dd";

string[] f=s.Split (new char[]{','},StringSplitOptions .RemoveEmptyEntries );

foreach (string e in f)

Console.WriteLine(e);

(3)

string s = "我是大评委 我是张三丰 我是王浑";

//string[] f = s.Split('我')只能劈开单个字符

string[] f = s.Split(new string[] { "我是" }, StringSplitOptions.RemoveEmptyEntries);

foreach (string e in f)

Console.WriteLine(e);

//可以劈开不确定的单个字符 即可以劈开任意个数的任意字符(劈开的个数和字符可以是任意的)

string s = "我是大评委 我大张三丰 我是王浑";

string[] f = s.Split(new char[]{'是','三'},StringSplitOptions.RemoveEmptyEntries);

foreach (string e in f)

Console.WriteLine(e);

(4)//字符串中是否有子串 是否有敏感字符串 s.Contains("社会") || s.Contains("和谐")

(1)string s = "我们的社会真和谐啊!!!";

if (s.Contains("社会") || s.Contains("和谐"))

Console.WriteLine("禁止出现敏感词汇");

(2)敏感词汇进行*掩藏

string s = "社会真和谐啊,构造和谐社会!!!";

if (s.Contains("和谐"))

{

char[] f = s.ToCharArray();

for (int i = 0; i < f.Length; i++)

{

if (f[i] == '和' && f[i+1]=='谐')

{

f[i]='*';

f[i + 1] = '*';

}

}

string temp = new string(f);

Console.WriteLine(temp);

Console.ReadKey();

}

(5)全部小写

string s = "ChinaRen";

//字符串是不可变的,所以转换后的值通过返回值

Console.WriteLine(s.ToLower());

Console.ReadKey();

(6)去掉空格 Trim()去掉两边的空格

s.TrimStart()去掉左边开始的空格 s.TrimEnd()去掉右边结尾的空格

string s = " ab cd ";

Console.WriteLine("输出字符串的长度");

Console.WriteLine(s.Length);

Console.WriteLine("输出字符串");

Console.WriteLine(s);

string s1 = s.Trim();//去掉两边的空格(中间的不管)

Console.WriteLine("输出去掉空格后字符串的长度");

Console.WriteLine(s1.Length);

Console.WriteLine("输出去掉空格后的字符串");

Console.WriteLine(s1);

Console.ReadKey();

(7)//string.Join("|", values)

string[] values = { "aa", "bb", "cc" };

string s = string.Join("|", values);//用分隔符连接字符串成为数组。

Console.WriteLine(s);

(8)//s.Replace(oldstring,newstring ); 字符串替换

string s = "高山|长江|黄河|北京";

s= s.Replace("|", ",");

Console.WriteLine(s); 高山,长江,黄河,北京

(9)//s.Substring(startIndex, length) 开始的索引 截取长度

string s = "高山|长江|黄河|北京";

s = s.Substring(3, 3);

Console.WriteLine(s);

//三元运算符 i==5?"是":"否"

int i = 5;

string s=(i==5?"是":"否");

Console.WriteLine(s);

string s = "高山|长江";

s = s.Substring(2,s.Length-2>5 ? 5:s.Length-2);

// 或者 s = s.Substring(2,Math.Min(s.Length-2,5));

Console.WriteLine(s);

(10)//判断是否是E-mail

Console.WriteLine("请输入E-mail");

string eAd = Console.ReadLine();

string name="";

string filed="";

if ((!eAd.StartsWith("@")) && (!eAd.EndsWith("@")) && (eAd.Contains("@")))

{

int starAt = eAd.IndexOf("@");

name = eAd.Substring(0, starAt);

filed = eAd.Substring(starAt);

}

Console.WriteLine("The Name is:{0}", name);

Console.WriteLine("The Address is:{0}", filed);

Console.ReadKey();

13.读取磁盘文本里的内容

string[] lines = System.IO.File.ReadAllLines("c:/1.txt", Encoding.Default);

14.变量的作用域(变量的作用域是变量声明的)

变量名就像试验实例容器的编号,在一个实验室中,编号不能重复。如果去了更高一级的实验室,就不能和下属的实验室的容易编号重复。只要不出实验室,不同实验室之间的容器编号可以重复。

两个平行的for和两个嵌套的for。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值