例子:字符串“ftp://admin:[email protected]”是登陆ftp服务器的标准格式。其中admin是用户名,1111是密码,192.168.100.6是ip地址。请编写一段代码获取这个字符串中的用户名、密码、服务器地址。string str1 = @"ftp://admin:[email protected]";
char[] sp = {'/',':','@' };
string[] temps教程plit = str1.split(sp);
string username = tempsplit[3];
string password = tempsplit[4];
string ip = tempsplit[5];
string str1 = @"ftp://admin:[email protected]";
char[] sp = {'/',':','@' };
string[] tempsplit = str1.split(sp);
foreach (string s in tempsplit)
{
console.writeline(s);
}
注意:我在使用这个方法的时候,开始把第二个参数当成结束的位置,在调试的时候遇到很多困难。借此引起注意,不要犯这种低级的错误,在对一个方法不是完全了解的时候,可以先阅读msdn和baidu、google。string str1 = @"ftp://admin:[email protected]";
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);
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
}
}
创造与魔法 安卓版v1.0.0750
创造与魔法是一款开放世界手游,在游戏中玩家可探索这个奇妙的世
创造与魔法修改版 最新版v1.0.0750
创造与魔法无限点券版是款探索冒险游戏,该款游戏的操作还是蛮自
战争与文明官方版本 安卓版v1.7.16
战争与文明是一款由上海邮通科技有限公司开发的战争策略游戏,这
迷你世界0元领皮肤无限迷你币版 最新安卓版v1.43.0
迷你世界0元购买皮肤版是这款开放沙盒冒险建造游戏的特殊破解版
创造与魔法无限经验版 安卓版v1.0.0750
创造与魔法无限经验版是款可以改造环境,整个游戏的自由度还是蛮