简介:如何利用IndexOf和SubString两种方法处理字符串,indexofsubstring
简介:如何利用IndexOf和SubString两种方法处理字符串,indexofsubstring
平时工作中我们会经常用到字符串处理,这时候,字符串的一些基本操作就会大显身手,这些方法不难入手,但想要取得预期的效果也需要稍动头脑,下面,我将为大家介绍一下string类的常用方法,以及个人使用这些方法处理字符串的实例。
indexof() :在字符串中从前向后定位字符和字符串;所有的返回值都是指在字符串的绝对位置,如为空则为- 1
string test="asdfjsdfjgkfasdsfsgfhgjgfjgdddd";
test.indexof('d') =2 //从前向后 定位 d 第一次出现的位置
test.indexof('d',1) =2 //从前向后 定位 d 从第三个字符串 第一次出现的位置
test.indexof('d',5,2) =6 //从前向后 定位 d 从第5 位开始查,查2位,即 从第5位到第7位;
lastindexof() :在字符串中从后向前定位字符和字符串;、
用法和 indexof() 完全相同。
下面介绍 IndexOfAny ||lastindexofany
他们接受字符数组做为变元,其他方法同上,返回数组中任何一个字符最早出现的下标位置
如下
char[] bbv={'s','c','b'};
string abc = "acsdfgdfgchacscdsad";
Response.Write(abc.IndexOfAny(bbv))=1
Response.Write(abc.IndexOfAny(bbv, 5))=9
Response.Write(abc.IndexOfAny(bbv, 5, 3))=9
====================================================================
substring() 用法
string a="aadsfdjkfgklfdglfd"
a.substring(5) //截取从第五位以后的所有字符串
a.substring(0,5) //截取从第0位置开始长度为5的字符串
代码实例:
效果:因为获取位置时用到的是成对位置,所以在处理时是成对处理
源码:
class Program
{
static void Main(string[] args)
{
try
{
string s = "<div>sfsfdf<;....'';;.asda.sds;.dasdssfa's/d.,zx>asd<>>><<<.";
Console.Out.WriteLine("测试字符串:" + s);
Console.Out.WriteLine("取>和<之间的所有字符:" + GetString(s, ">", "<"));
Console.Out.WriteLine("删除<和>之间的所有字符:" + DeleteString(s,"<", ">"));
}
catch { }
Console.Out.WriteLine("处理完毕!");
Console.Read();
}
//获取两个字符或字符串之间的信息,source传入的源字符串,取在sub_start和sub_end中间的所有字符,不包括sub_start和sub_end
public static String GetString(string source,string sub_start,string sub_end) {
string result = "";
int length;
//两个链表用来存放位置
List<int> L_start = new List<int>();
List<int> L_end = new List<int>();
//取所有sub_start位置
for (int i = 0; i < source.Length;i++)
{
if (source.IndexOf(sub_start, i, source.Length - i) == -1)
break;
else
{
i = source.IndexOf(sub_start, i, source.Length - i);
L_start.Add(i+sub_start.Length);//位置不加sub_start.Length会把sub_start也取到
}
}
//取所有sub_end位置
for (int i = 0; i < L_start.Count; i++)
{
if (source.IndexOf(sub_end, L_start[i], source.Length - L_start[i]) == -1)
break;
else
{
L_end.Add(source.IndexOf(sub_end, L_start[i], source.Length - L_start[i]));
}
}
//比较,取小值
if (L_start.Count <= L_end.Count)
length = L_start.Count;
else
length = L_end.Count;
//取字符串中间的值
for (int i = 0; i < length; i++)
{
result += source.Substring(L_start[i], L_end[i] - L_start[i]);
}
return result;
}
//删除两个字符或字符串之间的信息,source传入的源字符串,删除在sub_start和sub_end中间的所有字符,包括sub_start和sub_end
public static String DeleteString(string source, string sub_start, string sub_end)
{
string result = source;
string dinfo = "";
int length;
//两个链表用来存放位置
List<int> L_start = new List<int>();
List<int> L_end = new List<int>();
//取所有sub_start位置
for (int i = 0; i < source.Length; i++)
{
if (source.IndexOf(sub_start, i, source.Length - i) == -1)
break;
else
{
i = source.IndexOf(sub_start, i, source.Length - i);
L_start.Add(i);
}
}
//取所有sub_end位置
for (int i = 0; i < L_start.Count; i++)
{
if (source.IndexOf(sub_end, L_start[i], source.Length - L_start[i]) == -1)
break;
else
{
L_end.Add(source.IndexOf(sub_end, L_start[i], source.Length - L_start[i]) + sub_end.Length);
//不加sub_end.Length无法删除sub_end
}
}
//比较,取小值
if (L_start.Count <= L_end.Count)
length = L_start.Count;
else
length = L_end.Count;
//取字符串中间的值
for (int i = 0; i < length; i++)
{
dinfo = source.Substring(L_start[i], L_end[i] - L_start[i]);
result=result.Replace(dinfo,"");
}
return result;
}
}
相关文章
- 暂无相关文章
用户点评