asp.net 提高字串操作效率方法

來源:互聯網
上載者:User
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();

在對字串操作的時候,還有一把銳利的“瑞士軍刀”—Regex。等我學習了以後再來分享學習的成果

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="flg">true:加...,flase:不加</param>
/// <returns></returns>
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 截取字串+…
/// <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="flg"></param>
/// <returns></returns>
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編碼
/// <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
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.