C#中的取字符串的子串

现在有一字符串: "1234567/54345 " 

我现在要取 "/ "后面的 "54345 "

string   s= "1234567/54345 "; 
string[]   arr   =   s.Split(new   char[]{ '/ '});//   返回字符串数组{ "12345567 ", "54345 "} 
要取54345只需调用arr[1]就可以了 

string   s= "1234567/54345 "; 
string   substring=s.SubString(s.LastIndexOf( "/ ")+1);

Split函数返回的是一个由分割符(比如/)分割的字符串数组, 
例如: 
        输入                                 分割符                                         返回值 
"42,   12,   19 "                         new   Char[]   { ', ',   '   '}         { "42 ",   " ",   "12 ",   " ",   "19 "}   
"42..12..19 "                         new   Char[]   { '. '}                   { "42 ",   " ",   "12 ",   " ",   "19 "}   
"Banana "                                 new   Char[]   { '. '}                   { "Banana "}   
"Darb\nSmarba "                     new   Char[]   {}                         { "Darb ",   "Smarba "}   
"Darb\nSmarba "                     null                                           { "Darb ",   "Smarba "}   
"12/123/456 "                         new   Char[]   { '/ '}                   { "12 ", "123 ", "456 "} 
到Msdn上去查一下吧System.String类 

C#,查找字符串子串可以通过多种方法实现,这些方法提供不同的功能和效率。以下是一些常用的字符串查找方法: 1. `IndexOf` 方法:这是一个非常常用的方法,用于查找子串字符串的位置。如果找到子串,它会返回子串的起始索引;如果没有找到,则返回 -1。 示例代码: ```csharp string source = "Hello, world!"; int index = source.IndexOf("world"); // 返回索引为 7 ``` 2. `LastIndexOf` 方法:与 `IndexOf` 类似,但它是从字符串的末尾开始搜索,并返回子串最后出现的位置。 3. `Contains` 方法:检查字符串是否包含指定的子串。它返回一个布尔值。 示例代码: ```csharp string source = "Hello, world!"; bool contains = source.Contains("world"); // 返回 true ``` 4. `StartsWith` 和 `EndsWith` 方法:分别用来检查字符串是否以特定的子串开始或结束。 5. 使用正则表达式:`Regex` 类可以用来查找字符串的匹配项,适用于复杂的模式匹配。 示例代码: ```csharp using System.Text.RegularExpressions; string source = "Hello, world!"; Match match = Regex.Match(source, "world"); if (match.Success) { int startIndex = match.Index; // 返回索引为 7 } ``` 6. `Substring` 方法:虽然这个方法本身不查找子串,但它经常与 `IndexOf` 或其他方法一起使用来获子串。 示例代码: ```csharp string source = "Hello, world!"; int index = source.IndexOf("world"); string sub = source.Substring(index); // 返回 "world!" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值