asp中的InStr函数的改写

本文介绍了一种使用C#实现的字符串搜索算法,包括两个方法:InStrCShape用于基本的子串查找;InStrCShape2则提供更高级的功能,如指定起始位置和比较方式等。通过具体示例展示了如何调用这些方法并获取搜索结果。

<script type="text/C#" runat="server">
int? InStrCShape(string str1, string str2)
{
if (str1 == null || str2 == null)
return null;
return str1.IndexOf(str2)+1;
}
int? InStrCShape2(int? start, string str1, string str2, int? compare) {
int? result = null;
if (compare != null && start == null) {
throw new Exception("If you assign compare type,you must set start index!");
}
if (start != null && start < 1) {
throw new Exception("start index can not less than 1");
}
if (start == null){
start = 1;
}
if (compare == null) {
compare = 0;
}
if (str1 == null || str2==null ) {
return null;
}
if (str1.Length == 0) {
return 0;
}
if (str2.Length == 0) {
return start;
}
if (start > 1) {
str1 = str1.Substring(Convert.ToInt32(start));
}
int idx=str1.IndexOf(str2);
if (compare == 1) {
idx = str1.ToLower().IndexOf(str2.ToLower());
}
if (start > 1)
result = idx == -1 ? 0 : start + idx + 1;
else
result = idx == -1 ? 0 : idx + 1;
return result;
}
</script>

<%
string SearchString ="XXpXXpXXPXXP";
string SearchChar = "P";

int? l = 0;
l = InStrCShape2(4, SearchString, SearchChar, 1);
Response.Write(l);
l = InStrCShape2(1, SearchString, SearchChar, 0);
Response.Write(l);
l = InStrCShape2(null, SearchString, SearchChar, null);
Response.Write(l);
l = InStrCShape2(null, SearchString, "W", null);
Response.Write(l);
l = InStrCShape(SearchString,"");
Response.Write(l);
%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值