C#字符串:
1:定义一个字符串
string one, two;
one = "hello";
two = " gc";
2:使用 + 链接字符串
// 字符串链接
string str = one + two;
Console.WriteLine(str); // 输出hello gc
3:string类的属性
Chars获取固定值在某个字符串中的位置(这个如何使用,需要研究)
char local = one.Chars[1];
Console.WriteLine(local);
Length 在当前的 String 对象中获取字符数。
int num = one.Length;
Console.WriteLine(num);// 5
4:string类的方法
Compare 比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。该方法区分大小写。
就是比较哪里不一样,一样返回0,剩下都是不一样
string three = "Name is gc";
string four = "name is gc";
int html = String.Compare(three,four);
// Console.WriteLine(html); // 返回1
如果传了第三个参数布尔值为真,则不区分大小写。
int htmls = String.Compare(three, four,true);
Console.WriteLine(htmls); // 返回0
Concat 连接两个 string 对象(此处最多链接四个对象)
string con = String.Concat(three,four);
Console.WriteLine(con); //输出:Name is gcname is gc
Contains 查询特定字符串是否在另一个字符串中出现过。
bool res = three.Contains("is");
Console.WriteLine(res); // 返回True
Copy 复制当前字符串并赋值到一个新的字符串中
string strr = String.Copy(three);
Console.WriteLine(strr); // Name is gc
CopyTo 从 string 对象的指定位置开始复制指定数量的字符到 Unicode 字符数组中的指定位置。
string[] str_arr = { "a","b","c","d"};
string[] asds_arr = new string[6]; // 建立一个能存储六个值的空数组
str_arr.CopyTo(asds_arr, 2); // 从asds_arr数组第二个值开始赋值
foreach (var item in asds_arr)
{
//Console.WriteLine(item); // 输出 ' , ,a,b,c,d'
}
EndsWith 判断 string 对象的结尾是否匹配指定的字符串。
bool op = three.EndsWith("c");
Console.WriteLine(op); //输出True
Equals 判断当前的 string 对象是否与指定的 string 对象具有相同的值。区分大小写
string five = "asd";
string six = "azs";
//bool ol = String.Equals(five,six);
bool ol = String.Equals(four, three);
Console.WriteLine(ol); // 返回false
Format把指定字符串中一个或多个格式项替换为指定对象的字符串表示形式。
DateTime dts = new DateTime(2017, 4, 1, 13, 16, 32, 108);
string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", dt); //2017-04-01 13:16:32.108
IndexOf 返回指定字符串在该实例中第一次出现的索引,索引从 0 开始。
int azx = five.IndexOf("f");
Console.WriteLine(azx); //因为f不在five这个字符串中,所以返回-1
当此函数有两个参数时,返回指定字符串从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。
int qwer = three.IndexOf("is",1);
Console.WriteLine(qwer); // 返回值是5
Insert 返回一个新的字符串,其中,指定的字符串被插入在当前 string 对象的指定索引位置。
string eee = six.Insert(1,"qwe");
Console.WriteLine(eee); // aqwezs
IsNullOrEmpty 指示指定的字符串是否为 null 或者是否为一个空的字符串。
string wer = "";
bool resu = String.IsNullOrEmpty(wer);
Console.WriteLine(resu); // true
将数组转换成字符串(类似PHP implode方法)指定后两个参数,从第几位开始取,取几位
string[] arr = { "hello", " ", "gc" ,"hello","world"};
string mes = String.Join(" ", arr);
//Console.WriteLine(mes); // 输出 hello gc hello world(此处中间三个空格)
string mew = String.Join(" ", arr,3,2);
Console.WriteLine(mew); // 输出 hello world
LastIndexOf 返回指定字符串在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。
string avb = "bjgsdfjhsdf";
int plk = avb.LastIndexOf("f");
Console.WriteLine(plk); // 输出10
Remove 删除字符串中的字符,两个参数,从哪开始删,删几位
string lkj = avb.Remove(3,3);
Console.WriteLine(lkj); // bjfjhsdf
Replace 把当前 string 对象中,所有指定的字符串替换为另一个指定的字符串,并返回新的字符串。
string vfr = "qwertyuiop";
string bgt = vfr.Replace("qwerty","123");
Console.WriteLine(bgt); // 输出123uiop
StartsWith 判断字符串实例的开头是否匹配指定的字符串。
string cde = "my is is isw him";
bool sdfg = cde.StartsWith("my");
Console.WriteLine(sdfg); // 输出true
ToLower 把字符串转换为小写并返回
string rtyh = "asdFGH";
string qazxs = rtyh.ToLower();
Console.WriteLine(qazxs); // 输出 asdfgh
ToUpper 把字符串转换为大写并返回。
string wsd = rtyh.ToUpper();
Console.WriteLine(wsd); // 输出 ASDFGH
Trim 移除当前 String 对象中的所有前导空白字符和后置空白字符。
string jhg = " 123456 ";
string sd = jhg.Trim();
测试使用完整代码:我这里使用的是控制台应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// C#中的字符串
string one, two;
one = "hello";
two = " gc";
// 字符串链接
string str = one + two;
// Console.WriteLine(str); // 输出hello gc
// 获取当前时间
string time = DateTime.Now.ToString();
// Console.WriteLine(time); // 2019\1\22 星期二 15:31:08
// 使用字符串函数格式化时间
DateTime dt = new DateTime(2017, 4, 1, 13, 16, 32, 108);
string da = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", dt); //2017-04-01 13:16:32.108
// Console.WriteLine(da);
// 获取固定值在某个字符串中的位置(这个如何使用,需要研究)
//char local = one.Chars[1];
//Console.WriteLine(local);
// Length 在当前的 String 对象中获取字符数。
int num = one.Length;
// Console.WriteLine(num);// 5
// Compare 比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。该方法区分大小写。
// 就是比较哪里不一样,一样返回0,剩下都是不一样
string three = "Name is gc";
string four = "name is gc";
int html = String.Compare(three,four);
// Console.WriteLine(html); // 返回1
// 如果传了第三个参数布尔值为真,则不区分大小写。
int htmls = String.Compare(three, four,true);
//Console.WriteLine(htmls); // 返回0
// Concat 连接两个 string 对象(此处最多链接四个对象)
string con = String.Concat(three,four);
//Console.WriteLine(con); //输出:Name is gcname is gc
// Contains 查询特定字符串是否在另一个字符串中出现过。
bool res = three.Contains("is");
//Console.WriteLine(res); // 返回True
// Copy 复制当前字符串并赋值到一个新的字符串中
string strr = String.Copy(three);
// Console.WriteLine(strr); // Name is gc
// CopyTo 从 string 对象的指定位置开始复制指定数量的字符到 Unicode 字符数组中的指定位置。
string[] str_arr = { "a","b","c","d"};
string[] asds_arr = new string[6]; // 建立一个能存储六个值的空数组
str_arr.CopyTo(asds_arr, 2); // 从asds_arr数组第二个值开始赋值
foreach (var item in asds_arr)
{
//Console.WriteLine(item); // 输出 ' , ,a,b,c,d'
}
// EndsWith 判断 string 对象的结尾是否匹配指定的字符串。
bool op = three.EndsWith("c");
//Console.WriteLine(op); //输出True
// Equals 判断当前的 string 对象是否与指定的 string 对象具有相同的值。
// 区分大小写
string five = "asd";
string six = "azs";
//bool ol = String.Equals(five,six);
bool ol = String.Equals(four, three);
//Console.WriteLine(ol); // 返回false
// 把指定字符串中一个或多个格式项替换为指定对象的字符串表示形式。
DateTime dts = new DateTime(2017, 4, 1, 13, 16, 32, 108);
string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", dt); //2017-04-01 13:16:32.108
// IndexOf 返回指定字符串在该实例中第一次出现的索引,索引从 0 开始。
int azx = five.IndexOf("f");
//Console.WriteLine(azx); //因为f不在five这个字符串中,所以返回-1
//当此函数有两个参数时,返回指定字符串从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。
int qwer = three.IndexOf("is",1);
//Console.WriteLine(qwer); // 返回值是5
// Insert 返回一个新的字符串,其中,指定的字符串被插入在当前 string 对象的指定索引位置。
string eee = six.Insert(1,"qwe");
//Console.WriteLine(eee); // aqwezs
// IsNullOrEmpty 指示指定的字符串是否为 null 或者是否为一个空的字符串。
string wer = "";
bool resu = String.IsNullOrEmpty(wer);
//Console.WriteLine(resu); // true
// 将数组转换成字符串(类似PHP implode方法)指定后两个参数,从第几位开始取,取几位
string[] arr = { "hello", " ", "gc" ,"hello","world"};
string mes = String.Join(" ", arr);
//Console.WriteLine(mes); // 输出 hello gc hello world(此处中间三个空格)
string mew = String.Join(" ", arr,3,2);
Console.WriteLine(mew); // 输出 hello world
// LastIndexOf 返回指定字符串在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。
string avb = "bjgsdfjhsdf";
int plk = avb.LastIndexOf("f");
Console.WriteLine(plk); // 输出10
// Remove 删除字符串中的字符,两个参数,从哪开始删,删几位
string lkj = avb.Remove(3,3);
Console.WriteLine(lkj); // bjfjhsdf
// Replace 把当前 string 对象中,所有指定的字符串替换为另一个指定的字符串,并返回新的字符串。
string vfr = "qwertyuiop";
string bgt = vfr.Replace("qwerty","123");
Console.WriteLine(bgt); // 输出123uiop
// StartsWith 判断字符串实例的开头是否匹配指定的字符串。
string cde = "my is is isw him";
bool sdfg = cde.StartsWith("my");
Console.WriteLine(sdfg); // 输出true
// ToLower 把字符串转换为小写并返回
string rtyh = "asdFGH";
string qazxs = rtyh.ToLower();
Console.WriteLine(qazxs); // 输出 asdfgh
//ToUpper 把字符串转换为大写并返回。
string wsd = rtyh.ToUpper();
Console.WriteLine(wsd); // 输出 ASDFGH
// Trim 移除当前 String 对象中的所有前导空白字符和后置空白字符。
string jhg = " 123456 ";
string sd = jhg.Trim();
Console.WriteLine(sd); // 输出123456
}
}
}
有好的建议,请在下方输入你的评论。
欢迎访问个人博客
https://guanchao.site
欢迎访问小程序: