C#处理字符串——String.Substring的用法

需求:已知某个字符串,该字符串以“/”分割,需要获取到“/”前后的子串。

字符串样例:pengzhuang_22/pengzhuang_30,需要获得pengzhuang_22,以及pengzhuang_30

本文目录  隐藏

一、拆分字符串测试用例1

实际开发部分代码

二、获取子字符串测试用例2

三、获取子字符串测试用例3

四、获取子字符串测试用例4

五、获取子字符串测试用例5

六、获取子字符串测试用例6

一、拆分字符串测试用例1

String[] pairs = { "Color1=red", "Color2=green", "Color3=blue",
                 "Title=Code Repository" };
foreach (var pair in pairs)
{
    int position = pair.IndexOf("=");
    if (position < 0)
        continue;
    Console.WriteLine("Key: {0}, Value: '{1}'",
                   pair.Substring(0, position),
                   pair.Substring(position + 1));
}

// The example displays the following output:
//     Key: Color1, Value: 'red'
//     Key: Color2, Value: 'green'
//     Key: Color3, Value: 'blue'
//     Key: Title, Value: 'Code Repository'

实际开发部分代码

string str = XML.AppData.Instance.ListDeviceInterFace[i].ObjName;
foreach(var substr in str){
    int position = str.IndexOf("/");//以某个符号分隔前后两个子串
    if(position < 0)//判断
        continue;
    string TargetInterfaceName = str.Substring(position + 1);//获取的是/后的子串
    Debug.LogError(TargetInterfaceName);//打印输出结果
}

二、获取子字符串测试用例2

string[] info = { "Name:Charlie", "Title:Test"};//定义字符串,""中默认是一个字符串
int found = 0;//初始化found为0

Console.WriteLine("Test string:");
foreach (string s in info)//遍历info
    Console.WriteLine(s);

Console.WriteLine("\nA new test string:");
foreach (string s in info)//遍历info
{
    found = s.IndexOf(":");//此时的found的索引是:所在的位置
    Console.WriteLine("{0}", s.Substring(found + 1));//从:后开始算起,输出冒号后内容
}

输出:
// Test string:
// Name:Charlie
// Title:Test

// A new test string:
// Charlie
// Test

三、获取子字符串测试用例3

String value = "Hello,World!";
int startIndex = 0;//索引起始位置
int length = 5;//需要获得字符串的长度
String substring = value.Substring(startIndex, length);
Console.WriteLine(substring);

输出:
Hello

四、获取子字符串测试用例4

String myString = "abc";
bool test1 = myString.Substring(0, 1).Equals("a"); //true.
Console.WriteLine(test1);
bool test2 = String.IsNullOrEmpty(myString.Substring(3, 0)); //true.
Console.WriteLine(test2);
try
{
    string str3 = myString.Substring(2, 1);
    Console.WriteLine(str3);
}
catch (ArgumentOutOfRangeException e)
{
    Console.WriteLine(e.Message);
}

输出:
True
True
c

换种思路:如果将try catch中的string str3 = myString.Substring(2, 1);改为string str3 = myString.Substring(3, 1);会有什么输出?

答案是:Index and length must refer to a location within the string. Parameter name: length

五、获取子字符串测试用例5

String s = "aabbccdd";
Char charRange = 'b';
int startIndex = s.IndexOf(charRange);
int endIndex = s.LastIndexOf(charRange);
int length = endIndex - startIndex + 1;
Console.WriteLine("{0}.Substring({1}, {2}) = {3}",s, startIndex, length,s.Substring(startIndex, length));

输出:
aabbccdd.Substring(2, 2) = bb

六、获取子字符串测试用例6

String s = "<a>example<b>new string</b></a>";
String searchString = "<b>";
int startIndex = s.IndexOf(searchString);//<b>所在的索引位置
searchString = "</" + searchString.Substring(1);//</<b>new string</b></a>
int endIndex = s.IndexOf(searchString);//</所在的索引位置
String substring = s.Substring(startIndex);//<b>new string</b></a>
String substring1 = s.Substring(endIndex);//</b></a>
String substring2 = s.Substring(startIndex,endIndex + searchString.Length - startIndex);//<b>new string</b>
Console.WriteLine(substring);
Console.WriteLine(substring1);
Console.WriteLine(substring2);

输出:
<b>new string</b></a>
</b></a>
<b>new string</b>
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    请登录后查看评论内容