1、类的复习
类中有:
1)构造函数:初始化对象,给对象每个属性依次赋值;没有返回值,没有void,名称与类名一样;
2)字段:存储数据,设置每个对象的内容;
3)属性:保护字段,对字段的取值和设置进行限定;
4)方法:描述对象的行为,给定功能;
2、命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
如List list = new List();引用了using System.Collections.Generic;当删除时,想要添加命名空间的提示,方法1:鼠标停留在那里,左下角提示;方法2:“Alt+Shift+F10”
在一个项目中引用另一个项目中的类,需要添加手动右击:
接着引用命名空间:
3、值类型和引用类型,栈和堆
之间区别:
1)值类型和引用类型在内存上存储的地方不一样;
2)在传递值类型和传递引用类型时,传递的方式不一样;
值类型我们称之为值传递,引用类型我们称之为引用传递。
上一篇中介绍了人为划分的五大区域:堆、栈、静态存储区域、 。。。
而栈存储这些值类型,堆存储引用类型的。
值类型:int、double、bool、char、decimal、struct、enum
引用类型:string、自定义类、数组
4、字符串的不可变性
当给字符串重新赋值时,旧值没有销毁,而是重新开辟空间,堆中的地址增加一个,栈中指向新堆的地址。
当程序结束,GC扫描所有内存,发现地方没有任何指向,则清除掉。
可以将字符串数组看成是char类型的只读数组,通过下标访问字符串中的某个元素,不可以赋值。
若非常想改变字符串的内容,首先将其转为char:
再将字符数组char[]转为字符串:
5、解决字符串空间占用的浪费
StringBuilder解决的是字符串无效时的浪费问题,
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string s1 = null;
//创建时间计时器
Stopwatch sw = new Stopwatch();
sw.Start();//开始计时
for (int i = 0; i < 10000; i++)
{
s1 += i;
}
sw.Stop();//结束计时
Console.WriteLine(sw.Elapsed);
Console.ReadKey();
}
}
}
使用后:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
//创建StringBuilder
StringBuilder sb = new StringBuilder();
string s1 = null;
//创建时间计时器
Stopwatch sw = new Stopwatch();
sw.Start();//开始计时
for (int i = 0; i < 10000; i++)
{
sb.Append(i);//这么像Python
}
sw.Stop();//结束计时
//sb.ToString();
Console.WriteLine(sw.Elapsed);
Console.ReadKey();
}
}
}
原因:string每次都要开空间,StringBuilder都是同一块内存
6、解决字符串大小写问题
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("s1=");
string s1 = Console.ReadLine();
Console.WriteLine("s2=");
string s2 = Console.ReadLine();
Console.WriteLine("s1={0},s2={1}", s1, s2);
if (s1.Equals(s2, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("相同");
}
else
{
Console.WriteLine("不相同");
}
Console.ReadKey();
}
}
}
7、字符串的分割过滤
把字符串里不需要的东西过滤掉:
去除为空的位置:
拼接字符串数组:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
string s1 = "q d 3 4 . , + w / , d 3 ee ssa aq -";
char[] ch = { ' ', '+', '-', '/', '3', '4', '.', ',' };
string[] str=s1.Split(ch,StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < str.Length; i++)
{
sb.Append(str[i]);
}
Console.WriteLine(sb.ToString());
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string s1 = "2008-08-08";
string[] str = s1.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("{0}年{1}月{2}日",str[0],str[1],str[2]);
Console.ReadKey();
}
}
}
8、字符串的截取
9、字符串是否包含某字
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string s1 = "关键人物——老赵";
if (s1.Contains("老赵"))
{
s1=s1.Replace("老赵", "**");
}
Console.WriteLine(s1);
Console.ReadKey();
}
}
}
10、以什么结尾,以什么结束
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string s1 = "关键人物——老赵";
if (s1.StartsWith("关键"))
{
Console.WriteLine("start yes");
}
if (s1.EndsWith("老赵"))
{
Console.WriteLine("end y");
}
else
{
Console.WriteLine("no");
}
Console.ReadKey();
}
}
}
11、index of 查找char在字符串中的序号
查找所有的字符序号:
方法1:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string s1 = "老关老键老人老物——老赵";
int i=0;
while (i < s1.LastIndexOf('老'))
{
int current_i = s1.IndexOf('老',i);
Console.WriteLine(current_i);
i = current_i;
i++;
}
Console.ReadKey();
}
}
}
方法2:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入字符串:");
string str = Console.ReadLine();
Console.WriteLine("请输入要查找的字:");
string serach = Console.ReadLine();
Console.WriteLine("查找到的标号:");
int index = str.IndexOf(serach);
while (index != -1)
{
Console.WriteLine(index);
index = str.IndexOf(serach, index + 1);
}
Console.ReadKey();
}
}
}
寻找最后的文件名:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\s\e\f\d\s\w\e\老是\ddd\老赵\老赵.awv";
int i = path.LastIndexOf("\\");
string filename = path.Substring(i+1);
Console.WriteLine(filename);
Console.ReadKey();
}
}
}
12、Trim去空格
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string s1 = " a ";//前后空格
string s2 = " h ";//前空格
string s3 = " w ";//后空格
Console.Write(s1.Trim());
Console.Write(s2.TrimStart());
Console.Write(s3.TrimEnd());
Console.ReadKey();
}
}
}
13、string.IsNullOrEmpty
1、
2、
3、
14、string.Join
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string[] names = {"张三","李四","王五","赵六"};
string str=string.Join("||", names);
Console.WriteLine(str);
Console.ReadKey();
}
}
}
15、综合例子
1、读取txt文档,打印书目和作者
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Users\Administrator\Desktop\test.txt";
string[] fileContents = File.ReadAllLines(path,Encoding.Default);//与Python类似
for (int i = 0; i < fileContents.Length; i++)
{
char[] ch = { ' '};
string[] content = fileContents[i].Split(ch,StringSplitOptions.RemoveEmptyEntries);
if (content[0].Length >= 10)
{
Console.WriteLine("{0}...||作者:{1}", content[0].Substring(0, 8),content[1]);
}
else
{
Console.WriteLine("{0}...||作者:{1}", content[0], content[1]);
}
}
Console.ReadKey();
}
}
}
2、反转字符
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入字符串:");
string str = Console.ReadLine();
char[] charry = str.ToCharArray();
for (int i = 0; i < str.Length/2; i++)
{
char temp = charry[i];
charry[i] = charry[str.Length -1- i];
charry[str.Length -1- i] = temp;
}
str =new string(charry);
Console.WriteLine(str);
Console.ReadKey();
}
}
}
3、反转字符串单词
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入字符串:");
string str = Console.ReadLine();
char[] ch = {' '};//string[] strArray = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string[] strArray = str.Split(ch, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < strArray.Length/2; i++)
{
string temp = strArray[i];
strArray[i] = strArray[strArray.Length - 1 - i];
strArray[strArray.Length - i - 1] = temp;
}
str = string.Join(" ", strArray);
Console.WriteLine(str);
Console.ReadKey();
}
}
}