当前位置:网站首页>Study notes - C string delete character
Study notes - C string delete character
2022-07-20 15:42:00 【Remote music】
Method 1:s = s.Substring(0,s.Length - 1); Delete the last character of the string ;
s = s.Substring(0,s.Length - 1)
extend :
(1)String.Substring (Int32 index) : Reserve string subscripts index After the string ;
eg:s.Substring(3);
For from s Subscript position is 3 Start of character , Character substring after interception ,3 Indicates the start bit of the intercepted substring ;
s by :“asCded”;x by :“ded”;
(2)String.Substring (Int32 index1, Int32 index2) : Keep subscript from string index1 From character of , The length is index2 The string of ;
eg:string x=s.ubstring(3,2);
x For from s Subscript position is 3 Start of character , The intercepted length is 2 Character substring of ;
3 Indicates the starting position of the intercepted substring ,2 Indicates the length of the intercepted substring ;
s by :“asCded”;x by :“de”;
Method 2:s=s.Trim(); Delete the space at the beginning and end , Encounter the end of the first non space character ;
s=s.Trim();
extend :
(1)String.Trim(char myChar): Remove the matching characters from the front and back of the string respectively myChar The character of a character , Until the first mismatch of traversal myChar The characters in the character set , Stop traversing the removal operation ;
Trim If you don't fill in the following parentheses , By default, space characters are removed .
eg:
y = s.Trim();
s by :“ 075 pop ”;x by :“075 pop”;
y = s.Trim('_');
s by :“_075 pop_”;x by :“075 pop”;
eg:
y = s.Trim();
s by :“ 075 pop ”;x by :“075 pop”;
y = s.Trim('_');
s by :“_075 pop_”;x by :“075 pop”;
(2)String.Trim(char[ ] myChar): Remove the matching characters from the front and back of the string respectively myChar Characters in the character set , Until the first mismatch of traversal myChar The characters in the character set , Stop traversing the removal operation ;
eg:
char[] myChar= {'1','2','3','5','6','7','8','9','0'};// No, ‘4’;
x =s.Trim(myChar);
s by :“238ase 075 pop uuu2416”;x by :“ase 075 pop uuu24”;
eg:
char[] myChar= {'1','2','3','5','6','7','8','9','0'};// No, ‘4’;
x =s.Trim(myChar);
s by :“238ase 075 pop uuu2416”;x by :“ase 075 pop uuu24”;
char Character interpretation :
myChar Is included 0-9, But does not include 4 Set of characters ;
s.Trim(myChar); explain :
Is from the string s Inside out to inside ( Two ends to the middle ) Iterate through the string in turn , The traversal characters match mychar The character set is deleted , Until I meet the first one who doesn't belong to myChar The characters in the character set , Stop traversing the delete operation .
Process explanation :
1) Start from the front ,‘2’,‘3’,‘8’, All in myChar Character set , They all match , Delete these three characters ;
2) When you traverse to ‘a’ This character , and myChar There is no match in the character set , Stop operation here . The following characters are no longer traversed ;
3) meanwhile , Also start from the end of the string ,‘6’,‘1’, Energy and harmony myChar The characters in the character set match , Delete these two characters .
4) When you traverse to ‘4’ When this character , and myChar There is no match in the character set , Stop operation from here , The previous characters are no longer traversed and checked .
5) So the final result is :“ase 075 pop uuu24”;
(3)String.TrimStart(char[ ] myChar): Remove the characters at the beginning of the string
Same as Tirm(), Empty in parentheses , The default space character ;
eg1:
y = s.TrimStart();
s by :“ 075 pop ”;x by :“075 pop ”;
eg2:
char[] myChar= {'1','2','3','5','6','7','8','9','0'};// No, ‘4’;
x =s.TrimStart(myChar);
s by :“238ase 075 pop uuu2416”;x by :“ase 075 pop uuu2416”;
For detailed explanation, please refer to the above Trim();
(4)String.TrimEnd(char[ ] myChar): Remove the characters at the end of the string
Same as Tirm(), Empty in parentheses , The default space character ;
eg1:
y = s.TrimEnd();
s by :“ 075 pop ”;x by :“ 075 pop”;
eg2:
char[] myChar= {'1','2','3','5','6','7','8','9','0'};// No, ‘4’;
x =s.TrimEnd(myChar);
s by :“238ase 075 pop uuu2416”;x by :“238ase 075 pop uuu24”;
For detailed explanation, please refer to the above Trim() and TrimStart();
eg1:
y = s.TrimEnd();
s by :“ 075 pop ”;x by :“ 075 pop”;
eg2:
char[] myChar= {'1','2','3','5','6','7','8','9','0'};// No, ‘4’;
x =s.TrimEnd(myChar);
s by :“238ase 075 pop uuu2416”;x by :“238ase 075 pop uuu24”;
Method 3:s = s.Remove(s.Length-1,1); Start with the last bit of the string , Remove a character .( That is, remove the last character )
eg: p = s.Remove(s.Length - 1,1);
s by "awxno";p by “awxn”
string s = "ab_eeK_";
p = s.Remove(s.Length - 1,1);
//p by :"ab_eeK"; Last “_” It's been removed ;
extend :
(1)string.Remove(Int32 beginIndex);
Delete the string from beginIndex All characters after subscript start ;( Only keep in the string beginIndex Previous characters );
Eg:
p = a.Remove(3);
a by :“123abscOpq”;p by :“123”;
p = a.Remove(3);
a by :“123abscOpq”;p by :“123”;
(2)string.Remove(Int32 beginIndex, Int32 length);
Delete string beginIndex After subscript length Characters of length ;
Eg:
p = a.Remove(3,2);
a by :“123abscOpq”;p by :“123scOpq”;
p = a.Remove(3,2);
a by :“123abscOpq”;p by :“123scOpq”;
边栏推荐
猜你喜欢
【大型电商项目开发】缓存-分布式锁-缓存一致性解决-45
clickhouse 20.x 与prometheus + grafana的集成(三)
【WSN通信】基于matlab生物地理学优化HWSN节能聚类协议【含Matlab源码 1989期】
[mindspore] [read graph data] cannot read graph data in mindrecord format
openresty lua-resty-mlcache多级缓存
Almost died suddenly! An employee of an Internet company in Hangzhou was sent to ICU ward for myocarditis after staying up late and working overtime continuously. The company: she worked overtime volu
关于链游系统开发(智能合约上链原理分析说明)丨NFT链游系统开发原理分析及案例
【大型电商项目开发】商城业务-检索服务-搭建页面环境-47
PLC-LiSLAM线-面-圆柱体-激光SLAM
软件测试如何快速入门
随机推荐
ZFS - 01 - 创建与扩容zpool基本操作
Only 22 years old! This Post-00 doctor plans to work in 985 universities!
web安全入门-icmp测试与防御
QGIS mosaic tile grid data
Pytorch, used by the nonzero instance
Pytorch, initialize tensor
TFIDF实例及讲解
Pytorch, filter out a certain range of values
⾦融衍⽣产品是什么?
Notes in October 2020
Pymongo migrates data from mongodb database
2022 Henan League game 2: Henan University of Technology
2022河南联赛第(二)场:河南理工大学
【MATLAB项目实战】基于MATLAB的发票识别(含GUI界面)
文件同步工具 rsync 常用选项使用介绍及通过服务同步数据
广州深入开展经营性自建房安全整治“百日行动” 两个月排查房屋逾两百万栋
Bleu score implementation
pytorch,筛选出一定范围的值
Unity - invincible hero (high energy ahead)
图像标注开源小工具-labelImg