简单获取字符串中间值。
暂时只知道这种简单的方法。
菜鸟方法,
1
public
string
substringone(
string
firstkey,
string
lastkey,
string
str)
2 {
3 int startIndex = str.IndexOf(firstkey);
4 int lastIndex = str.IndexOf(lastkey);
5 string newstr = str.Substring(startIndex, lastIndex);
6 return substringlast(firstkey, lastkey, newstr);
7 }
8
9 private string substringlast( string firstkey, string lastkey, string str)
10 {
11 int startIndex = str.IndexOf(firstkey);
12 int lastIndex = str.IndexOf(lastkey);
13 return str.Substring(startIndex, lastIndex).Replace(firstkey, "" );
14 }
2 {
3 int startIndex = str.IndexOf(firstkey);
4 int lastIndex = str.IndexOf(lastkey);
5 string newstr = str.Substring(startIndex, lastIndex);
6 return substringlast(firstkey, lastkey, newstr);
7 }
8
9 private string substringlast( string firstkey, string lastkey, string str)
10 {
11 int startIndex = str.IndexOf(firstkey);
12 int lastIndex = str.IndexOf(lastkey);
13 return str.Substring(startIndex, lastIndex).Replace(firstkey, "" );
14 }
不知道还有没有其他方法。暂时记录在这里,如果有什么其他方法 希望大家留言给我 感激不尽。
调用。
1
static
void
Main(
string
[] args)
2 {
3 string str = " <?xml version=\"1.0\" encoding=\"GB2312\"?> <EnterpriseInfo EnterpriseNumber=\"74260279\" EnterpriseName=\"**********\" EnterpriseShortName=\"***********\" EnterprisePhone=\"*********\" EnterpriseAddress=\********\" EnterpriseEmail=\"*************\" > " ;
4 string fristkey = " seNumber=\" " ;
5 string lastKey = " \" EnterpriseName " ;
6 substringone(fristkey, lastKey, str);
7 }
2 {
3 string str = " <?xml version=\"1.0\" encoding=\"GB2312\"?> <EnterpriseInfo EnterpriseNumber=\"74260279\" EnterpriseName=\"**********\" EnterpriseShortName=\"***********\" EnterprisePhone=\"*********\" EnterpriseAddress=\********\" EnterpriseEmail=\"*************\" > " ;
4 string fristkey = " seNumber=\" " ;
5 string lastKey = " \" EnterpriseName " ;
6 substringone(fristkey, lastKey, str);
7 }