当前位置:网站首页>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
边栏推荐
- LwIP learning socket (API)
- Wpf: solve the problem that materialdesign:dialoghost cannot be closed
- Quality blog——
- P1896 [SCOI2005] 互不侵犯(状压dp)
- 超限黑客认知
- STM32F103 SPI (pit Diary)
- 多旅行商问题——公式和求解过程概述
- 【LeetCode】4. Best time to buy and sell stock
- Luaframwrok handles resource updates
- P2704 [noi2001] artillery position (shape pressure DP)
猜你喜欢
Redis batch startup and shutdown script
Iterm2 setting
Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task
IP production stream is so close to me
Pat class a 1031 Hello world for u
haproxy+keepalived集群搭建02
My touch screen production "brief history" 2
什么是数据类型?数据类型有什么用?
Docker installs MySQL and successfully uses Navicat connection
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
随机推荐
CLion-Toolchains are not configured Configure Disable profile问题解决
regular expression
Client server model
C language learning notes (mind map)
Static keyword
Redis batch startup and shutdown script
Are you still watching the weather forecast on TV?
Install cross compiler arm none liunx gnueabihf
【LeetCode】2. Valid parentheses · valid parentheses
static关键字
LwIP learning socket (API)
Idea dereference display effect
[cocos creator] Click the button to switch the interface
Transfinite hacker cognition
Unity one click AssetBundle
[MySQL 12] MySQL 8.0.18 reinitialization
Technical dry goods | some thoughts on the future of AI architecture
Luaframwrok handles resource updates
Redis view client connection
数据库应用技术课程设计之商城管理系统