python字符串操作,以及对应的C#实现
--IndexOf--
python:
inx = str.find("aa")
c#:
var inx = str.IndexOf("aa");
--LastIndexOf--
python
inx = str.rfind("aa")
c#
var inx = str.LastIndexOf("aa");
--Remove--
python
str = "asdfg" str = str[3:] #fg
c#
var str = "asdfg"; str = str.Remove(0, 3); //fg
--反向Remove--
python
str = "asdfg" str = str[:-2] #asd
c#
var str = "asdfg"; str = str.Remove(str.Length-2, 2); //asd
--SubString取中间段--
python
前后均为索引
str = "asdfg" str = str[1:3] #sd
c#
var str = "asdfg"; str = str.Substring(1, 2); //sd
--TrimStart, TrimEnd--
python
def main(): print "qwe_xxxxx".lstrip("qwe_") #Output:xxxxx #lstrip对应TrimStart #rstrip对应TrimEnd #strip为统一删除 #如果不填写参数,默认删除/r/n这类符号
C#
var str = "qwe_xxxxx"; str = str.TrimStart("qwe_") //Output:xxxxx