当前位置:网站首页>String class
String class
2022-07-03 08:05:00 【Luckyᕙ(⇀‸↼‵‵)ᕗ】
== and Equals The difference between
1、 Value type
== and Equals The contents of the comparison are consistent , It's all comparative content
string a = "123";// value
Console.WriteLine(a=="123");
Console.WriteLine(a.Equals("123"));
2、 Reference type
==: Compare whether the first address is equal Equals: The comparison is whether the values are equal
char[] x={ '1','2','3'};
string b = newstring(x);// quote
Console.WriteLine(b=="123");
Console.WriteLine(b.Equals("123"));
1) String comparison
Compare(str1, str2)
str1.CompareTo(str2)
string a = "hello",b="Hello",c="hello";
Console.WriteLine(a.CompareTo(b));//-1
Console.WriteLine(string.Compare(a,b));//-1
Console.WriteLine(a.CompareTo(c));//0
- String lookup
(1)Contains(Findstr): Find whether the specified string contains a string Findstr, The return value of bool type , That is, only
true and false.
(2)、IndexOf(Findstr): lookup FindStr First occurrence in string , The return value is the first occurrence
The subscript , If not found, return -1.
(3)、LastIndexOf(FindStr): lookup FindStr Last occurrence in string , Return value is last
The subscript of one occurrence , If not found, return -1.
Console.WriteLine(a.Contains("lo"));//true
Console.WriteLine(a.IndexOf('e'));//1
Console.WriteLine(a.LastIndexOf('l'));//3
- String truncation
SubString(StartIndex): Subscript in string from StartIndex Start all subsequent strings .
SubString(StartIndex, Len): Subscript in string from StartIndex Start the back Len A string of length .
Console.WriteLine(a.Substring(1));//ello
Console.WriteLine(a.Substring(1,3));//ell
- Segmentation of strings
Split(SplitCh): The string Press SplitCh Segmentation , Its return value is an array of strings .
string d=" Hoe standing grain gradually pawning a midday # Sweat dripping under the grass # Who knows what's on the plate # Every grain is hard ";
string[] e = d.Split('#');
for (int i = 0; i < e.Length; i++)
{
Console.WriteLine(e[i]);
}
- String merging
string.Concat(str1, str2, …., strn): take n String connection , There is no connector in the middle
“+” Connection symbol
string f = "world";
Console.WriteLine(a+f);
Console.WriteLine(1+2+3+a);//6hello
Console.WriteLine(a+1+2+3);//hello123
Console.WriteLine(string.Concat(a,f));//helloworld
- String replacement
Replace(oldStr, newStr): use newStr To replace... In the string oldStr
Console.WriteLine(a.Replace("he","HE"));//HEllo
- String insertion
Insert(index, str):index Is where you need to insert ,str Is the character to insert
Console.WriteLine(a.Insert(1,"ww"));//hwwello
- Deletion of string
Trim(): Delete the spaces at the beginning and end of the string
Remove(Start):Start For the starting position to be deleted , The method is to delete all characters after the position
Remove(Start, Len):Len It refers to the length to be deleted , Different from the above format, it starts from the starting position
Delete Len Characters
string s = " aaa bbb ccc ";
Console.WriteLine(s.Trim());//aaa bbb ccc
Console.WriteLine(s.Remove(6));// aaa b
Console.WriteLine(s.Remove(2,7));// aaccc
9) String case conversion
ToLower(): Convert the string to lowercase
ToUpper(): Convert string to uppercase
10)string.Format()
string.Format("{0:d}",System.DateTime.Now) // The result is :2021-3-23( The location of the month is not 03)
string.Format("{0:D}",System.DateTime.Now) // The result is :2021 year 3 month 23 Japan
string.Format("{0:f}",System.DateTime.Now) // The result is :2021 year 3 month 23 Japan 15:37
string.Format("{0:F}",System.DateTime.Now) // The result is :2021 year 3 month 23 Japan 15:37:52
string.Format("{0:g}",System.DateTime.Now) // The result is :2021-3-23 15:38
string.Format("{0:G}",System.DateTime.Now) // The result is :2021-3-23 15:39:27
string.Format("{0:m}",System.DateTime.Now) // The result is :3 month 23 Japan
string.Format("{0:t}",System.DateTime.Now) // The result is :15:41
string.Format("{0:T}",System.DateTime.Now) // The result is :15:41:50
边栏推荐
- Conversion between JSON and object
- Redis profile
- How does yarn link help developers debug NPM packages?
- Uniapp learning records
- Technical dry goods | some thoughts on the future of AI architecture
- jsutlis
- Transplantation of tslib Library
- Multi traveling salesman problem -- overview of formula and solution process
- IP production stream is so close to me
- Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
猜你喜欢
![[MySQL 12] MySQL 8.0.18 reinitialization](/img/e1/9874df18bbc8d80c3c5c5fe39aefc9.png)
[MySQL 12] MySQL 8.0.18 reinitialization

Unity2019_ Lighting system

一个实习生的CnosDB之旅

Scite change background color

PostGIS space function

Redis batch startup and shutdown script

What is a data type? What is the use of data types?

Storage of data

the installer has encountered an unexpected error installing this package
![[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)](/img/de/1c75b5e7ed79aca47462de365428a7.jpg)
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
随机推荐
2020-12-12
Lua framwrok framework starts
Pat class a 1031 Hello world for u
Transplantation of tslib Library
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
regular expression
Idea dereference display effect
Use filechannel to copy files
idea取消引用顯示效果
oracle 插入单引号
Shader foundation 01
PHP common sorting algorithm
P2622 关灯问题II(状态压缩 搜索)
C language learning notes (mind map)
数据的存储
E: Unable to locate package ROS melody desktop full
YOLO系列 --- xml2txt脚本
Conversion between JSON and object
Luaframwrok handles resource updates
使用 FileChannel 进行文件的复制拷贝