【www.gdgbn.com--淘宝规则】

1、分割字符串,所用方法split。 原话是这样讲的,分割字符串是指将已有的字符串按照一定的规则进行分割,以获取新形式的子字符串。
例子:字符串“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];
注意:为什么username是tempsplit[3]开始的呢?不清楚的话,可以通过以下代码进行验证。
string str1 = @"ftp://admin:1111@192.168.100.6";
char[] sp = {"/",":","@" };
string[] tempsplit = str1.split(sp);
foreach (string s in tempsplit)
{
console.writeline(s);
}
2、提取字符串,所用方法substring。 还是上面的那个例子,这次我用的是检索字符串的方法。msdn上说,string表示文本,一系列unicode字符,char表示一个unicode字符。
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);
注意:我在使用这个方法的时候,开始把第二个参数当成结束的位置,在调试的时候遇到很多困难。借此引起注意,不要犯这种低级的错误,在对一个方法不是完全了解的时候,可以先阅读msdn和baidu、google。
3、构造(合并)字符串,所用方法append。 这里需要用到的是stringbuild类,stringbuild类是存储可变字符串值的类。合并字符串时可以不用额外声明一个字符串变量来存储结果。
比如我在构造一个由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
{
///


/// 常用函数基类
///

public class funobject
{
#region 替换字符串
///
/// 功能:替换字符
///

/// 字符串
/// 替换掉"的字符串
public static string filtersql(string strvalue)
{
string str = "";
str = strvalue.replace("""", "");
return str;
}
#endregion
#region 对表 表单内容进行转换html操作,
///
/// 功能:对表 表单内容进行转换html操作,
///

/// html字符串
///
public static string htmlcode(string fstring)
{
string str = "";
str = fstring.replace(">", ">");
str = fstring.replace("<", "<");
str = fstring.replace(" ", " ");
str = fstring.replace("n", "
");
str = fstring.replace("r", "
");
str = fstring.replace("rn", "
");
return str;
}
#endregion
#region 判断是否:返回值:√ or ×
///
/// 判断是否:返回值:√ or ×
///

/// true 或false
/// √ or ×
public static string judgement(bool b)
{
string s = "";
if (b == true)
s = "";
else
s = "×";
return s;
}
#endregion
#region 截取字符串
///
/// 功能:截取字符串长度
///

/// 要截取的字符串
/// 字符串长度
/// true:加...,flase:不加
///
public static string getstring(string str, int length, bool flg)
{
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 (flg)
str += "......";
break;
}
j++;
}
return str;
}
#endregion
#region 截取字符串+…
///
/// 截取字符串+…
///

///
///
///
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 字符串分函数
///
/// 字符串分函数
///

///
///
///
///
public string stringsplit(string strings, int index, string separ)
{
string[] s = strings.split(char.parse(separ));
return s[index];
}
#endregion
#region 分解字符串为数组
///
/// 字符串分函数
///

/// 要分解的字符串
/// 分割符,可以为string类型
/// 字符数组
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编码
///
/// url编码
///

/// 字符串
///
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中的配置字段值
///
/// 获取全局配置参数
///

/// 键名
/// 参数
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
///
/// 根据传入的字符串是否为yes/no返回bit
///

///
///
public static int getbitbool(string flg)
{
int str = 0;
switch (flg.tolower())
{
case "yes":
str = 1;
break;
case"no":
str = 0;
break;
default:
break;
}
return str;
}
#endregion
#region html编码
///
/// html编码
///

///
///
public static string htmlencode(string strinput)
{
string str;
try
{
str = httpcontext.current.server.htmlencode(strinput);
}
catch
{
str = "error";
}
return str;
}
///
/// html解码
///

///
///
public static string htmldecode(string strinput)
{
string str;
try
{
str = httpcontext.current.server.htmldecode(strinput);
}
catch
{
str = "error";
}
return str;
}
#endregion
#region 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
///
/// 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
///

/// 原始字符串
/// 目标字符串
///
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 检测字符串中是否含有中文及中文长度
///
/// 检测字符串中是否含有中文及中文长度
///

/// 要检测的字符串
/// 中文字符串长度
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 取字符串右侧的几个字符
///
/// 取字符串右侧的几个字符
///

/// 字符串
/// 右侧的几个字符
///
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 替换右侧的字符串
///
/// 替换右侧的字符串
///

/// 字符串
/// 右侧的字符串
/// 要替换为的字符串
///
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
}
}

本文来源:http://www.gdgbn.com/taobaodaxue/29928/