您的位置:首页 > 编程语言 > C#

/*C#:扩展方法*/ 《自学系列》

2016-05-04 15:01 567 查看
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 类库
{
public static class 字符处理
{
public static string[] 寻找字符(this string 字符串, string 分隔符 = " ", int 位置 = 0, bool 方向 = true, bool 选择 = true)
{/*实现字符串内以分隔符按位置顺序左向右向搜寻字符功能*/
string[] 返回字符组 = new string[2];
字符串 = 选择 ? 字符串.处理后(分隔符) : 字符串.处理前(分隔符);
if (方向)
{
while (字符串[位置] != 分隔符[0])
返回字符组[0] += 字符串[位置++].ToString();
返回字符组[1] = (位置 + 1).ToString();
}
else
{
while (字符串[位置] != 分隔符[0])
返回字符组[0] = 字符串[位置--].ToString() + 返回字符组[0];
返回字符组[1] = (位置 - 1).ToString();
}
return 返回字符组;
}
public static string 处理后(this string 字符串, string 分隔符)
{ return 字符串 + 分隔符; }
public static string 处理前(this string 字符串, string 分隔符)
{ return 分隔符 + 字符串; }
}
}

string 字符串 = "1 22 33 6 9 999 369";
字符串 = 字符串.Insert(2, "999 ");
int 位置 = 字符串.Length;
string[] 返回组;
do
{
//返回组 = 寻找字符(字符串, 选择: false, 位置: 位置);
返回组 = 字符串.处理前(" ").寻找字符(方向: false, 位置: 位置);/*学习C#:扩展方法*/
位置 = int.Parse(返回组[1]);/*以返回的值作为下一个调用*/
Console.WriteLine("返回{0},位置{1}", 返回组[0].ToString(), 位置);
} while (位置 > 0);
Console.WriteLine();


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: