asp.net 提高字符串操作效率方法
例子:字符串“ftp://admin:11111@192.168.100.6”是登陆ftp服务器的标准格式。其中admin是用户名,1111是密码,192.168.100.6是ip地址。请编写一段代码获取这个字符串中的用户名、密码、服务器地址。string str1 = @"ftp://admin:1111@192.168.100.6";
char[] sp = {'/',':','@' };
string[] temps教程plit = str1.split(sp);
string username = tempsplit[3];
string password = tempsplit[4];
string ip = tempsplit[5];
string str1 = @"ftp://admin:1111@192.168.100.6";
char[] sp = {'/',':','@' };
string[] tempsplit = str1.split(sp);
foreach (string s in tempsplit)
{
console.writeline(s);
}
注意:我在使用这个方法的时候,开始把第二个参数当成结束的位置,在调试的时候遇到很多困难。借此引起注意,不要犯这种低级的错误,在对一个方法不是完全了解的时候,可以先阅读msdn和baidu、google。string str1 = @"ftp://admin:1111@192.168.100.6";
int index1 = str1.indexof("ftp://") + 6;
int index2 = str1.lastindexof(":") + 1;
int index3 = str1.indexof("@") + 1;
int index4 = str1.length;
int usernamelength = index2 - index1 - 1;
int passwordlength = index3 - index2 - 1;
int iplength = index4 - index3;
string username = str1.substring(index1, usernamelength);
string password = str1.substring(index2, passwordlength);
string ip = str1.substring(index3, iplength);
console.writeline(username);
console.writeline(password);
console.writeline(ip);
比如我在构造一个由http发送的认证字符串时,我们可以用append方法来构造所要发送的数据报。
stringbuilder sb = new stringbuilder();
string username = "%d2%bb%c2%b7%bf%f1%ec%ad";
string password = "e10adc3949ba59abbe56e057f20f883e";
string str1 = "formhash=3e58f988&loginfield=username&username=";
string str2 = "&password=";
string str3 = "&questionid=0&answer=&cookietime=2592000";
sb.append(str1);
sb.append(username);
sb.append(str2);
sb.append(password);
sb.append(str3);
console.writeline(sb.tostring());
console.readkey();
在对字符串操作的时候,还有一把锐利的“瑞士军刀”—正则表达式。等我学习了以后再来分享学习的成果
asp教程.net 字符串操作基类(安全,替换,分解等)
* 1,取字符串右侧的几个字符
* 2,替换右侧的字符串
****************************************************************/
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.text;
namespace ec
{
/// <summary>
/// 常用函数基类
/// </summary>
public class funobject
{
#region 替换字符串
/// <summary>
/// 功能:替换字符
/// </summary>
/// <param name="strvalue">字符串</param>
/// <returns>替换掉'的字符串</returns>
public static string filtersql(string strvalue)
{
string str = "";
str = strvalue.replace("''", "");
return str;
}
#endregion
#region 对表 表单内容进行转换html操作,
/// <summary>
/// 功能:对表 表单内容进行转换html操作,
/// </summary>
/// <param name="fstring">html字符串</param>
/// <returns></returns>
public static string htmlcode(string fstring)
{
string str = "";
str = fstring.replace(">", ">");
str = fstring.replace("<", "<");
str = fstring.replace(" ", " ");
str = fstring.replace("n", "<br />");
str = fstring.replace("r", "<br />");
str = fstring.replace("rn", "<br />");
return str;
}
#endregion
#region 判断是否:返回值:√ or ×
/// <summary>
/// 判断是否:返回值:√ or ×
/// </summary>
/// <param name="b">true 或false</param>
/// <returns>√ or ×</returns>
public static string judgement(bool b)
{
string s = "";
if (b == true)
s = "<b><font color=#009900>√</font></b>";
else
s = "<b><font color=#ff0000>×</font></b>";
return s;
}
#endregion
#region 截取字符串
/// <summary>
/// 功能:截取字符串长度
/// </summary>
/// <param name="str">要截取的字符串</param>
/// <param name="length">字符串长度</param>
/// <param name="易做图">true:加...,flase:不加</param>
/// <returns></returns>
public static string getstring(string str, int length, bool 易做图)
{
int i = 0, j = 0;
foreach (char chr in str)
{
if ((int)chr > 127)
{
i += 2;
}
else
{
i++;
}
if (i > length)
{
str = str.substring(0, j);
if (易做图)
str += "......";
break;
}
j++;
}
return str;
}
#endregion
#region 截取字符串+…
/// <summary>
/// 截取字符串+…
/// </summary>
/// <param name="strinput"></param>
/// <param name="intlen"></param>
/// <returns></returns>
public static string cutstring(string strinput, int intlen)//截取字符串
{
asciiencoding ascii = new asciiencoding();
int intlength = 0;
string strstring = "";
byte[] s = ascii.getbytes(strinput);
for (int i = 0; i < s.length; i++)
{
if ((int)s[i] == 63)
{
intlength += 2;
}
else
{
intlength += 1;
}
try
{
strstring += strinput.substring(i, 1);
}
catch
{
break;
}
if (intlength > intlen)
{
break;
}
}
//如果截过则加上半个省略号
byte[] mybyte = system.text.encoding.default.getbytes(strinput);
if (mybyte.length > intlen)
{
strstring += "…";
}
return strstring;
}
#endregion
#region 字符串分函数
/// <summary>
/// 字符串分函数
/// </summary>
/// <param name="strid"></param>
/// <param name="index"></param>
/// <param name="separ"></param>
/// <returns></returns>
public string stringsplit(string strings, int index, string separ)
{
string[] s = strings.split(char.parse(separ));
return s[index];
}
#endregion
#region 分解字符串为数组
/// <summary>
/// 字符串分函数
/// </summary>
/// <param name="str">要分解的字符串</param>
/// <param name="splitstr">分割符,可以为string类型</param>
/// <returns>字符数组</returns>
public static string[] splitstr(string str, string splitstr)
{
if (splitstr != "")
{
system.collections.arraylist c = new system.collections.arraylist();
while (true)
{
int thissplitindex = str.indexof(splitstr);
if (thissplitindex >= 0)
{
c.add(str.substring(0, thissplitindex));
str = str.substring(thissplitindex + splitstr.length);
}
else
{
c.add(str);
break;
}
}
string[] d = new string[c.count];
for (int i = 0; i < c.count; i++)
{
d[i] = c[i].tostring();
}
return d;
}
else
{
return new string[] { str };
}
}
#endregion
#region url编码
/// <summary>
/// url编码
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
public static string urlencoding(string str)
{
byte[] bytes = system.text.encoding.utf8.getbytes(str);
return system.text.encoding.utf8.getstring(bytes).tostring();
}
#endregion
#region 获取web.config中的配置字段值
/// <summary>
/// 获取全局配置参数
/// </summary>
/// <param name="key">键名</param>
/// <returns>参数</returns>
public static string getapp(string key)
{
system.configuration.appsettingsreader appr = new system.configuration.appsettingsreader();
try
{
string str = (string)appr.getvalue(key, typeof(string));
if (str == null || str == "") return null;
return str;
}
catch (exception e) { }
return null;
}
#endregion
#region 根据传入的字符串是否为yes/no返回bit
/// <summary>
/// 根据传入的字符串是否为yes/no返回bit
/// </summary>
/// <param name="易做图"></param>
/// <returns></returns>
public static int getbitbool(string 易做图)
{
int str = 0;
switch (易做图.tolower())
{
case "yes":
str = 1;
break;
case"no":
str = 0;
break;
default:
break;
}
return str;
}
#endregion
#region html编码
/// <summary>
/// html编码
/// </summary>
/// <param name="strinput"></param>
/// <returns></returns>
public static string htmlencode(string strinput)
{
string str;
try
{
str = httpcontext.current.server.htmlencode(strinput);
}
catch
{
str = "error";
}
return str;
}
/// <summary>
/// html解码
/// </summary>
/// <param name="strinput"></param>
/// <returns></returns>
public static string htmldecode(string strinput)
{
string str;
try
{
str = httpcontext.current.server.htmldecode(strinput);
}
catch
{
str = "error";
}
return str;
}
#endregion
#region 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
/// <summary>
/// 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
/// </summary>
/// <param name="srcstring">原始字符串</param>
/// <param name="aimstring">目标字符串</param>
/// <returns></returns>
public static bool isenglish(string srcstring, string aimstring)
{
bool rev = true;
string chr;
if (aimstring == "" || aimstring == null) return false;
for (int i = 0; i < aimstring.length; i++)
{
chr = aimstring.substring(i, 1);
if (srcstring.indexof(chr) < 0)
{
return false;
break;
}
}
return rev;
}
#endregion
#region 检测字符串中是否含有中文及中文长度
/// <summary>
/// 检测字符串中是否含有中文及中文长度
/// </summary>
/// <param name="str">要检测的字符串</param>
/// <returns>中文字符串长度</returns>
public static int cnstringlength(string str)
{
asciiencoding n = new asciiencoding();
byte[] b = n.getbytes(str);
int l = 0; // l 为字符串之实际长度
for (int i = 0; i <= b.length - 1; i++)
{
if (b[i] == 63) //判断是否为汉字或全脚符号
{
l++;
}
}
return l;
}
#endregion
#region 取字符串右侧的几个字符
/// <summary>
/// 取字符串右侧的几个字符
/// </summary>
/// <param name="str">字符串</param>
/// <param name="length">右侧的几个字符</param>
/// <returns></returns>
public static string getstrright(string str, int length)
{
string rev = "";
if (str.length < length)
{
rev = str;
}
else
{
rev = str.substring(str.length - length, length);
}
return rev;
}
#endregion
#region 替换右侧的字符串
/// <summary>
/// 替换右侧的字符串
/// </summary>
/// <param name="str">字符串</param>
/// <param name="strsrc">右侧的字符串</param>
/// <param name="straim">要替换为的字符串</param>
/// <returns></returns>
public static string repstrright(string str, string strsrc, string straim)
{
string rev = "";
if (getstrright(str, strsrc.length) != strsrc)
{
rev = str;
}
else
{
rev = str.substring(0, str.length - strsrc.length).tostring() + straim.tostring();
}
return rev;
}
#endregion
}
}
补充:asp.net教程,.Net开发