例如
在 "nanfsk <user> " 中查找 "<user>" 所在的位置用什么函数?
还有 例如:要在"nadfa <user>张山 </user>"
中 获得 <user> 与 </user> 之间夹着的 文字(张山) 用什么方法比较好?
13 个解决方案
#1
IndexOf()
#2
LastIndexOf()
#3
string a="abcde";
string b="c";
a.IndexOf(b);
string b="c";
a.IndexOf(b);
#4
LastIndexOf() 比较好
#5
正则表达式!
也可以用字符串拆分的方法。
string str = "nadfa <user>张山 </user>";
string[] strArray = str.Split('>');
string[] strResult = strArray[1].Split('<');
strResult[0]就是结果“张山”
其中,Split('字符')就是用来根据字符来拆分字符串的函数,拆分的结果放到一个字符数组里面。
也可以用字符串拆分的方法。
string str = "nadfa <user>张山 </user>";
string[] strArray = str.Split('>');
string[] strResult = strArray[1].Split('<');
strResult[0]就是结果“张山”
其中,Split('字符')就是用来根据字符来拆分字符串的函数,拆分的结果放到一个字符数组里面。
#6
http://dev.csdn.net/Develop/article/41/41245.shtm
#7
LastIndexOf()
#8
see:
------------------------------------------
// Sample for String.IndexOf(String, Int32, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int end;
int count;
end = str.Length;
start = end/2;
Console.WriteLine();
Console.WriteLine("All occurences of 'he' from position {0} to {1}.", start, end-1);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");
count = 0;
at = 0;
while((start <= end) && (at > -1))
{
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
}
Console.WriteLine();
}
}
/*
This example produces the following results:
All occurences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
------------------------------------------
// Sample for String.IndexOf(String, Int32, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int end;
int count;
end = str.Length;
start = end/2;
Console.WriteLine();
Console.WriteLine("All occurences of 'he' from position {0} to {1}.", start, end-1);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");
count = 0;
at = 0;
while((start <= end) && (at > -1))
{
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
}
Console.WriteLine();
}
}
/*
This example produces the following results:
All occurences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
#9
LastIndexOf()
#10
楼上的可以简介一些嘛!
string str = "nadfa <user>张山 </user>";
int intS = 0;
string strName = "张山";
int intPos = str.IndexOf(strName, intS, strName.Length);
string str = "nadfa <user>张山 </user>";
int intS = 0;
string strName = "张山";
int intPos = str.IndexOf(strName, intS, strName.Length);
#11
用IndexOf可以实现了,你可以试试的!
#12
IndexOf()从前往后查
LastIndexOf()从后往前查
#13
up
#1
IndexOf()
#2
LastIndexOf()
#3
string a="abcde";
string b="c";
a.IndexOf(b);
string b="c";
a.IndexOf(b);
#4
LastIndexOf() 比较好
#5
正则表达式!
也可以用字符串拆分的方法。
string str = "nadfa <user>张山 </user>";
string[] strArray = str.Split('>');
string[] strResult = strArray[1].Split('<');
strResult[0]就是结果“张山”
其中,Split('字符')就是用来根据字符来拆分字符串的函数,拆分的结果放到一个字符数组里面。
也可以用字符串拆分的方法。
string str = "nadfa <user>张山 </user>";
string[] strArray = str.Split('>');
string[] strResult = strArray[1].Split('<');
strResult[0]就是结果“张山”
其中,Split('字符')就是用来根据字符来拆分字符串的函数,拆分的结果放到一个字符数组里面。
#6
http://dev.csdn.net/Develop/article/41/41245.shtm
#7
LastIndexOf()
#8
see:
------------------------------------------
// Sample for String.IndexOf(String, Int32, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int end;
int count;
end = str.Length;
start = end/2;
Console.WriteLine();
Console.WriteLine("All occurences of 'he' from position {0} to {1}.", start, end-1);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");
count = 0;
at = 0;
while((start <= end) && (at > -1))
{
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
}
Console.WriteLine();
}
}
/*
This example produces the following results:
All occurences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
------------------------------------------
// Sample for String.IndexOf(String, Int32, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int end;
int count;
end = str.Length;
start = end/2;
Console.WriteLine();
Console.WriteLine("All occurences of 'he' from position {0} to {1}.", start, end-1);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");
count = 0;
at = 0;
while((start <= end) && (at > -1))
{
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
}
Console.WriteLine();
}
}
/*
This example produces the following results:
All occurences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
#9
LastIndexOf()
#10
楼上的可以简介一些嘛!
string str = "nadfa <user>张山 </user>";
int intS = 0;
string strName = "张山";
int intPos = str.IndexOf(strName, intS, strName.Length);
string str = "nadfa <user>张山 </user>";
int intS = 0;
string strName = "张山";
int intPos = str.IndexOf(strName, intS, strName.Length);
#11
用IndexOf可以实现了,你可以试试的!
#12
IndexOf()从前往后查
LastIndexOf()从后往前查
#13
up