命名空间:System.String.Split

程序集:mscorlib( mscorlib.dll)

简单实例:

string before = "12,50,30";

string[] after =before.Split(new char[]{','});

//结果为 after[0] = 12;  after[1] = 50; after[2] = 30;

1.正则表达

如果字符串是混合模式,即同时含有不同的类型,可以使用以下的方法分割他们的元素。

using System;
using System.Text.RegularExpressions; public class Example
{
public static void Main()
{
String[] expressions = { "16 + 21", "31 * 3", "28 / 3",
"42 - 18", "12 * 7",
"2, 4, 6, 8" };
String pattern = @"(\d+)\s+([-+*/])\s+(\d+)";
foreach (var expression in expressions)
foreach (Match m in Regex.Matches(expression, pattern)) {
int value1 = Int32.Parse(m.Groups[1].Value);
int value2 = Int32.Parse(m.Groups[3].Value);
switch (m.Groups[2].Value)
{
case "+":
Console.WriteLine("{0} = {1}", m.Value, value1 + value2);
break;
case "-":
Console.WriteLine("{0} = {1}", m.Value, value1 - value2);
break;
case "*":
Console.WriteLine("{0} = {1}", m.Value, value1 * value2);
break;
case "/":
Console.WriteLine("{0} = {1:N2}", m.Value, value1 / value2);
break;
}
}
}
}
// The example displays the following output:
// 16 + 21 = 37
// 31 * 3 = 93
// 28 / 3 = 9.33
// 42 - 18 = 24
// 12 * 7 = 84

\s-

Match a whitespace character followed by a hyphen.

\s?

Match zero or one whitespace character.

[+*]?

Match zero or one occurrence of either the + or * character.

\s?

Match zero or one whitespace character.

-\s

Match a hyphen followed by a whitespace character.

using System;
using System.Text.RegularExpressions; public class Example
{
public static void Main()
{
String input = "[This is captured\ntext.]\n\n[\n" +
"[This is more captured text.]\n]\n" +
"[Some more captured text:\n Option1" +
"\n Option2][Terse text.]";
String pattern = @"\[([^\[\]]+)\]";
int ctr = 0;
foreach (Match m in Regex.Matches(input, pattern))
Console.WriteLine("{0}: {1}", ++ctr, m.Groups[1].Value);
}
}
// The example displays the following output:
// 1: This is captured
// text.
// 2: This is more captured text.
// 3: Some more captured text:
// Option1
// Option2
// 4: Terse text.

\[

Match an opening bracket.

([^\[\]]+)

Match any character that is not an opening or a closing bracket one or more times. This is the first capturing group.

\]

Match a closing bracket.

2.搜索指定的字符

using System;
using System.Collections.Generic; public class Example
{
public static void Main()
{
String value = "This is the first sentence in a string. " +
"More sentences will follow. For example, " +
"this is the third sentence. This is the " +
"fourth. And this is the fifth and final " +
"sentence.";
var sentences = new List<String>();
int position = 0;
int start = 0;
// Extract sentences from the string.
do {
position = value.IndexOf('.', start);
if (position >= 0) {
sentences.Add(value.Substring(start, position - start + 1).Trim());
start = position + 1;
}
} while (position > 0); // Display the sentences.
foreach (var sentence in sentences)
Console.WriteLine(sentence);
}
}
// The example displays the following output:
// This is the first sentence in a string.
// More sentences will follow.
// For example, this is the third sentence.
// This is the fourth.
// And this is the fifth and final sentence.

IndexOf , 返回某个特定字符或者字符串第一次出现的位置,which returns the zero-based index of the first occurrence of a character or string in a string instance.

IndexOfAny , 返回某个或多个特定字符或者字符串第一次出现的位置,which returns the zero-based index in the current string instance of the first occurrence of any character in a character array.

LastIndexOf ,返回某个特定字符或者字符串最后一次出现的位置 ,which returns the zero-based index of the last occurrence of a character or string in a string instance.

LastIndexOfAny, which returns a zero-based index in the current string instance of the last occurrence of any character in a character array.

string.IndexOf/string.LastIndexOf

IndexOf方法用于搜索在一个字符串中,某个特定的字符或者子串第一次出现的位置,该方法区分大小写,并从字符串的首字符开始以0计数。如果字符串中不包含这个字符或子串,则返回-1。

定位字符:
int IndexOf(char value)
int IndexOf(char value, int startIndex)
int IndexOf(char value, int startIndex, int count)

定位子串:
int IndexOf(string value)
int IndexOf(string value, int startIndex)
int IndexOf(string value, int startIndex, int count)

在上述重载形式中,其参数含义如下:
value:待定位的字符或者子串。
startIndex:在总串中开始搜索的其实位置。
count:在总串中从起始位置开始搜索的字符数

例如:

string str = "那么骄傲少爷的身子跑堂儿的命儿那么骄傲少爷的身子跑堂儿的命儿";
string str1 = str.IndexOf("百度").ToString();          //返回 -1
string str2 = str.IndexOf("少爷").ToString();          //返回 4
string str3 = str.IndexOf("少爷", 10).ToString();     //返回19 说明:这是从第10个字符开始查起。
string str4 = str.IndexOf("爷", 10, 5).ToString();     //返回 -1
string str5 = str.IndexOf("爷", 10, 20).ToString();   //返回 20 说明:从第10个字符开始查找,要查找的范围是从第10个字符开始后20个字符,即从第10-30个字符中查找。

同IndexOf类似,LastIndexOf用于搜索在一个字符串中,某个特定的字符或者子串最后一次出现的位置,其方法定义和返回值都与IndexOf相同,不再赘述。

IndexOfAny/LastIndexOfAny
IndexOfAny方法功能同IndexOf类似,区别在于,它可以搜索在一个字符串中,出现在一个字符数组中的任意字符第一次出现的位置。同样,该方法区分大小写,并从字符串的首字符开始以0计数。如果字符串中不包含这个字符或子串,则返回-1。常用的IndexOfAny重载形式有3种:

int IndexOfAny(char[]anyOf);
int IndexOfAny(char[]anyOf, int startIndex);
int IndexOfAny(char[]anyOf, int startIndex, int count)。
在上述重载形式中,其参数含义如下:
anyOf:待定位的字符数组,方法将返回这个数组中任意一个字符第一次出现的位置。
startIndex:在原字符串中开始搜索的其实位置。
count:在原字符串中从起始位置开始搜索的字符数。

例如:

String s = "Hello";
char[] anyOf = { 'H', 'e', 'l' };
int i1 = s.IndexOfAny(anyOf);       //返回 0
int i2 = s.LastIndexOfAny(anyOf);   //返回 3

同IndexOfAny类似,LastIndexOfAny用于搜索在一个字符串中,出现在一个字符数组中任意字符最后一次出现的位置

C#分割字符串的更多相关文章

  1. Delphi中stringlist分割字符串的用法

    Delphi中stringlist分割字符串的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 1.CommaT ...

  2. C语言分割字符串

    最近在做一道C语言题目的时候需要用到分割字符串,本来想自己手写的,也不会很麻烦,但想到其他语言都有分割字符串的库函数,C语言怎么会没有呢?所以,在网上搜了一搜,果然有这样的函数,还是很好用的,在此总结 ...

  3. Android--split()分割字符串特殊用法

    split()分割字符串 1.不同环境下的区分 Java:分割字符串不能写成split("$")//$为要分割的字符Android:分割字符串需要加上中括号split(" ...

  4. Sql Server分割字符串函数

    -- Description: 分割字符串函数 -- SELECT * FROM dbo.Split('a,b,c,d,e,f,g',',') -- ========================= ...

  5. lua string的自定义分割字符串接口

    -------------------------------------------------------------------- --  Create By  SunC 2014/7/1 -- ...

  6. 用C语言来分割字符串

    #include <stdio.h> int main() { ] = {}; ] = {}; ] = {}; sscanf("1,2,3#3,4#4,5"," ...

  7. JAVA 一个或多个空格分割字符串

    知识补充 String的split方法支持正则表达式: 正则表达式\s表示匹配任何空白字符,+表示匹配一次或多次. 有了以上补充知识,下面的内容就很好理解了. 一.待分割字符串 待分割字符串为如下: ...

  8. Swift3.0语言教程分割字符串与截取字符串

    Swift3.0语言教程分割字符串与截取字符串 Swift3.0语言教程分割字符串 如果想要快速的创建一个数组,我们可以将字符串进行分割,分割后的内容将会生成一个数组.在NSString中有两个分割字 ...

  9. LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件

    //file_op.c #include <string.h> #include <stdio.h> #include <stdlib.h> struct info ...

  10. SQLServer实现split分割字符串到列

    网上已有人实现sqlserver的split函数可将字符串分割成行,但是我们习惯了split返回数组或者列表,因此这里对其做一些改动,最终实现也许不尽如意,但是也能解决一些问题. 先贴上某大牛写的sp ...

随机推荐

  1. 浅谈输入输出”重定向“——基于Linux系统

    前言 进程在启动后会自动的打开3个文件:标准输入.标准输出和标准错误输出分别对应文件描述符0.1.2.对于每个进程他们都都维护了一张文件描述符表(file descriptor table),通常fd ...

  2. 151008-JS初级完成,PHP入门(变量常量等)-没假放了

    hi 今天是10月8号,你懂的,好困好困哒 上午搞定了JS的入门篇,真的是入门篇,基本都是JS做基本的输出.样式变化.惯例给出代码 <!DOCTYPE HTML><html>& ...

  3. 微软极品工具箱-Sysinternals Suite

    工具包由来 Sysinternals Suite是微软发布的一套非常强大的免费工具程序集,一共包括74个windows工具.Sysinternals是Winternals公司提供的免费工具,Winte ...

  4. Nginx负载均衡配置

    1.yum安装nginx yum install nginx 2.启动nginx chkconfig nginx on   service nginx start向web服务器中放入测试文件: < ...

  5. java 22 - 22 多线程之 匿名内部类的方式实现多线程

    首先回顾下之前的匿名内部类: 匿名内部类的格式: new 接口或者接口名(){ 重写方法 }; 本质:是该类或者接口的子类对象 匿名内部类方式使用多线程 1.new Thread(){代码-}.sta ...

  6. 转:设置Eclipse中的tab键为4个空格的完整方法

    from: https://my.oschina.net/xunxun10/blog/110074   设置Eclipse中的tab键为4个空格的完整方法 收藏 XunXun10 发表于 4年前 阅读 ...

  7. Eclipse 搜索插件 instasearch

    http://marketplace.eclipse.org/content/instasearch

  8. zlog学习笔记(zc_arraylist)

    zc_arraylist.h /** * 实现类似列表的功能 * */ #ifndef __zc_arraylist_h #define __zc_arraylist_h #define ARRAY_ ...

  9. css中如何设置字体

    来自百度的回答: 建议使用font-family: "Microsoft YaHei";支持UTF-8和GB2312字符集. 不生效的3种情况:1.当此属性定义的是全局样式时,对于 ...

  10. .net混淆、反编译工具调查

    常用的工具列表[比较常见的] 混淆器.加密 Dotfuscator VS默认带的工具,不过是个社区版 强度不大 dotNET Reactor 使用了NativeCode 和混淆的形式 Xenocode ...