1.字符串创建
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02字符串创建
{
internal class Program
{
static void Main(string[] args)
{
//对象类型: 可以通过class 创建一个对象类型
People p1 = new People() { Id=100}; //p1对象类型
//基本类型:int float string等 int a =10;
int a = 10;
Test(a);
Test1(p1);
//字符串创建的方法
//1字面量创建方式:是最简单的创建方式 建议使用的一种方式
string s1 = "hello world";
//2 字符串中有特殊的符号, \n 换行 ;\t tab; \r回车 \s space, \也是一个特殊字符
//\r将光标移回当前行的行首,但不换到新行。
string s2 = "翩若惊鸿,\n婉\t若游龙\r";
Console.Write(s2);
//要求字符串显示\n 需要对特殊符号进行转义处理 加一个\,
string s3 = "翩若惊鸿,\\n婉\\t若游龙\\r";
Console.WriteLine(s3);
//3 使用@符号创建字符串 保留字符串各种符号换行等效果
string s4 = @"\n\t\r可以支持换行
wolrd
name
change
“”";
Console.WriteLine(s4);
//string ssss = "C:\Users\Administrator\Desktop\2025.12.9C#基础语法(七)"; //但度加一个\ 字符串报错
// string ssss = "C:\\Users\\Administrator\\Desktop\\2025.12.9C#基础语法(七)";//修改 加上转义符号
string ssss = @"C:\Users\Administrator\Desktop\2025.12.9C#基础语法(七)";//使用@保留\
Console.WriteLine(ssss);
//4 字符串创建方式 通过new string构建字符串
char[] chars = new char[] { '老', '头', '乐' };//创建一个字符数组
string str5 = new string(chars);
Console.WriteLine(str5);
//5 使用$符号进行格式化
double gz = 1000000;
double age = 18;
string str6 = $"工资{gz},年龄{age}";
Console.WriteLine(str6);
//字符串属性 类似于特殊的数组 索引值是从0开始
string str7 = "的好时机是多少";
Console.WriteLine(str7.Length);//字符个数 7
Console.WriteLine(str7[str7.Length-1]);//少
//也可以遍历字符串
for (int i = 0; i < str7.Length; i++)
{
char s = str7[i];
Console.WriteLine(s);
}
Console.ReadKey();
}
static void Test(int c) //传递一个整数
{
}
static void Test1(People c) //传递一个people类型参数
{
}
static void Test(int c,int a,bool s)//三个参数没有任何关系
{
}
static void Test(Man a) //a整体
{
}
}
class People
{
public int Id { get; set; }
}
class Man
{
public int Id { get; set; }
public int Agency { get; set; }
public string Type { get; set; }
}
}
2.字符串常用的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_字符串常用的方法
{
internal class Program
{
static void Main(string[] args)
{
//字符串常用的方法
//1重点: indexof() 获取字符在字符串当中的索引值 如果找到了返回对应
//索引值,如果找不到返回值为-1,
string str = "abcdefgabc";
Console.WriteLine(str.IndexOf("a"));//0
Console.WriteLine(str.IndexOf("h"));//-1
Console.WriteLine(str.IndexOf("bc"));//1
Console.WriteLine(str.IndexOf('f',4));//5 从参数2地方开始搜索参数1
//2 lastIndexOf() 从后往前找 找出第一个与之匹配的字符
Console.WriteLine(str.LastIndexOf("a"));//7
//3IndexOfAny() 从字符数组这个范围中找任何一个对应的索引值,
Console.WriteLine(str.IndexOfAny(new char[] { 'c', 'b',}));//1
//4.LastIndexOfAny() 从后往前找,找到范围当中其中一个的索引值
Console.WriteLine(str.LastIndexOfAny(new char[] { 'c', 'b', }));//9
//5重点 .Contains() 是否包含参数
Console.WriteLine(str.Contains("fg")); //true
Console.WriteLine(str.Contains("he"));//false
//6重点 ToUpper() 把字符串转成大写的
Console.WriteLine(str.ToUpper());
//7重点 ToLower() 转成小写字母
Console.WriteLine(str.ToLower());
//8StartsWith() 判断字符串是否参数进行开头的
string str2 = "张三大师傅";
Console.WriteLine(str2.StartsWith("张三"));// true
Console.WriteLine(str2.StartsWith("张1三"));//false
//9 EndsWith() 判断是不是以...结尾
Console.WriteLine(str2.EndsWith("师傅")); //true
//10重点 IsNullOrEmpty() 判断字符串是不是null或者是空字符串
string str3 = "";
Console.WriteLine(string.IsNullOrEmpty(str3));//true
str3 = null;// 空的
Console.WriteLine(string.IsNullOrEmpty(str3));//true
str3 = " ";//空格字符串
Console.WriteLine(string.IsNullOrEmpty(str3));//false
//11 Equals() 判断俩个字符串是否相等 对象比较不要使用==,如果比较
//俩个对象时候尽量使用equals()方法
Console.WriteLine(string.Equals(str,str2));//false
//12join()把指定的分隔符号添加对应的字符串之间;
Console.WriteLine(string.Join("-", "a","b","c")); //a-b-c
//13重点 Substring() 截取字符串从索引值为1开始 截取3个长度
Console.WriteLine( str2.Substring(1,3));//三大师
//14重点Split() 按照指定的符号进行分割字符串
string str4 = "2025/12/10"; // 可以按照/进行分割,分割成三部分,三部分放到一个数组里面
Console.WriteLine(str4);
string[] ss = str4.Split('/');//结果是一个数组
Console.WriteLine(ss[0]); //2025
Console.WriteLine(ss[1]); //12
Console.WriteLine(ss[2]);//10
//15重点Concat() 拼接多个字符串
Console.WriteLine(string.Concat(str2, str4)) ; //张三大师傅2025 / 12 / 10
//16CopyTo() 从源字符串的参数1索引开始赋值,赋值到参数2数组里面,放到目标数
//组的索引值为参数3地方,复制参数4个元素
//"2025/12/10"
char[] c1 = new char[15];//定义一个字符数组长度为10
str4.CopyTo(1, c1, 5, 6);
for (int i = 0; i < c1.Length; i++)
{
Console.WriteLine(c1[i]+"-----------");
}
//17重点 Remove() 删除字符,从参数1开始删除,删除参数2个数,返回结果是剩余的
// Console.WriteLine(str4.Remove(1,4));
// Console.WriteLine(str4.Remove(3));// 只写一个参数的删除到最后 剩余的字符
//18Replace() 用参数2替换参数1
Console.WriteLine(str4.Replace("2025", "2026"));
//19 Format()字符串格式化
string name = "张三";
double gz = 10000;
Console.WriteLine(string.Format("姓名{0};工资{1}", name, gz));
Console.WriteLine(string.Format("{0:c4}",gz));//¥10,000.0000
//20 ToCharArray() 把字符串转成一个个字符组成数组
for (int i = 0; i < str4.ToCharArray().Length; i++)
{
Console.WriteLine(str4.ToCharArray()[i]+"--------");
}
/*常用的字符串的方法
* 1 indexof()
* 2 Contains()
* 3 ToUpper()
* 4 ToLower()
* 5 StartsWith()
* 6 IsNullOrEmpty()
* 7 Equals()
* 8 Substring()
* 9 Split()
* 10 Remove()
* 11 Concat()
*/
Console.ReadKey();
}
}
}
3.数组
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04数组
{
internal class Program
{
static void Main(string[] args)
{
//数组:是一个引用类型,目的存储相同类型的数据,可以通过索引值取出数组的元素,
//数组是有顺序的,顺序是从0开始,也可以对数组进行遍历等操作
//数组是有长度的,最大索引值等于长度-1;
// 数组命名尽量名称后面加一个s,能够开发者知道是一个数组, 例如int[] nums, string[] names
//定义数组
//1 字面量定义数组(快速定义数组)
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; //创建已经添加数组的元素
//2 定义可以带数组的长度
string[] strs = new string[6]; //长度为6的字符串数组
//3可以定义带初始元素和长度
char[] chars = new char[6] { 'A', 'A' ,'A', 'A', 'A','B' };
//4 可以定义数组不带长度 只带初始元素的 ,随着初始化元素个数设置数组的长度
double[] doubles = new double[] {1,2,3};
//如何给数组添加元素
//strs[0] = "黎元洪"; 单独添加元素
//strs[1] = "袁世凯"; 单独添加元素
//使用for循环添加
for (int i = 0; i < strs.Length; i++)
{
strs[i] = "鲁班" + i + "号";
}
// 获取数组的长度
Console.WriteLine(strs.Length);//6
Console.WriteLine(strs[0]); //获取其中一个元素
//遍历数组
for (int i = 0; i < strs.Length; i++)
{
Console.Write(strs[i]+"--");
}
//遍历方式2
// int 数组元素类型
// i 每一个元素
//in 在哪个数组
//nums 数组
foreach (int i in nums)
{
Console.WriteLine(i+"?");
}
// int[] 一维数组
//数组包含:一维数组、多维数组、交错数组
Console.ReadKey();
}
}
}