1. split the string using the split method.In this case, the delimiter refers to dividing an existing string according to certain rules to obtain a new form of substring.
Example: the string "ftp: // admin: 11111@192.168.100.6" is the standard format for logging on to the ftp server. Here, admin is the user name, 1111 is the password, and 192.168.100.6 is the IP address. Write a code to obtain the username, password, and server address in the string.
String str1 = @ "ftp: // admin: 1111@192.168.100.6 ";
Char [] sp = {'/',':','@'};
String [] temps tutorial plit = str1.split (sp );
String username = tempsplit [3];
String password = tempsplit [4];
String ip = tempsplit [5];
Note:Why does username start with tempsplit [3? If you do not know, you can use the following code for verification.
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. Extract the string using the substring method.In the above example, I used the string retrieval method this time. According to msdn,
StringIndicates text,
A seriesUnicode characters,
CharIndicates
OneUnicode character.
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 );
Note:When using this method, I started to regard the second parameter as the end position, and encountered many difficulties during debugging. Do not make such low-level mistakes. When you are not fully familiar with a method, read msdn, baidu, and google first.
3. Construct (merge) the string using append.What is needed here is
StringbuildClass,
StringbuildClass is the class that stores variable string values. When merging strings, you do not need to declare an additional string variable to store the results.
For example, when constructing an authentication string sent by http, we can use the append method to construct the datagram to be sent.
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();
When operating on strings, there is also a sharp "Swiss Army Knife"-regular expression. I will share my learning results later.
Asp tutorial. net string operation base class (security, replacement, decomposition, etc)
* 1. Take the characters on the right of the string.
* 2. Replace the string on the right.
**************************************** ************************/
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.html controls;
Using system. text;
Namespace ec
{
/// <Summary>
/// Common function base class
/// </Summary>
Public class funobject
{
# Region replacement string
/// <Summary>
/// Function: replace characters
/// </Summary>
/// <Param name = "strvalue"> string </param>
/// <Returns> replace 'string </returns>
Public static string filtersql (string strvalue)
{
String str = "";
Str = strvalue. replace ("''","");
Return str;
}
# Endregion
# Region converts the table content to html,
/// <Summary>
/// Function: Convert the table content to html,
/// </Summary>
/// <Param name = "fstring"> html string </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: returns √ or ×
/// <Summary>
/// Determine whether: Return Value: √ or ×
/// </Summary>
/// <Param name = "B"> true or 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 truncated string
/// <Summary>
/// Function: truncates the string length.
/// </Summary>
/// <Param name = "str"> string to be intercepted </param>
/// <Param name = "length"> String length </param>
/// <Param name = "flg"> true: add..., flase: do not add </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 truncation string +...
/// <Summary>
/// Capture string +...
/// </Summary>
/// <Param name = "strinput"> </param>
/// <Param name = "intlen"> </param>
/// <Returns> </returns>
Public static string cutstring (string strinput, int intlen) // truncates a string
{
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;
}
}
// If yes, add half ellipsis
Byte [] mybyte = system. text. encoding. default. getbytes (strinput );
If (mybyte. length> intlen)
{
Strstring + = "... ";
}
Return strstring;
}
# Endregion
# Region string Score Function
/// <Summary>
/// String Function
/// </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 decomposition string is an array
/// <Summary>
/// String Function
/// </Summary>
/// <Param name = "str"> string to be decomposed </param>
/// <Param name = "splitstr"> delimiter, which can be string type </param>
/// <Returns> character array </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 Encoding
/// <Summary>
/// Url Encoding
/// </Summary>
/// <Param name = "str"> string </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 obtain the configuration field value in web. config
/// <Summary>
/// Obtain global configuration parameters
/// </Summary>
/// <Param name = "key"> key name </param>
/// <Returns> parameter </returns>
Public static string getapp (string key)
{
System. configuration. appsettingsreader appr = new system. configuration. etettingsreader ();
Try
{
String str = (string) appr. getvalue (key, typeof (string ));
If (str = null | str = "") return null;
Return str;
}
Catch (exception e ){}
Return null;
}
# Endregion
# Region returns a bit based on whether the input string is yes/no
/// <Summary>
/// Bit is returned based on whether the input string is yes/no
/// </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 Encoding
/// <Summary>
/// Html Encoding
/// </Summary>
/// <Param name = "strinput"> </param>
/// <Returns> </returns>
Public static string htmlencode (string strinput)
{
String str;
Try
{
Str = httpcontext.current.server.html encode (strinput );
}
Catch
{
Str = "error ";
}
Return str;
}
/// <Summary>
/// Html Decoding
/// </Summary>
/// <Param name = "strinput"> </param>
/// <Returns> </returns>
Public static string htmldecode (string strinput)
{
String str;
Try
{
Str = httpcontext.current.server.html decode (strinput );
}
Catch
{
Str = "error ";
}
Return str;
}
# Endregion
# Region checks whether a character is included in another character. If yes, true is returned. Otherwise, false is returned.
/// <Summary>
/// Check whether a character is in another character. If yes, true is returned. Otherwise, false is returned.
/// </Summary>
/// <Param name = "srcstring"> original string </param>
/// <Param name = "aimstring"> Target string </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 check whether the string contains Chinese characters and Chinese characters
/// <Summary>
/// Check whether the string contains Chinese characters and Chinese characters
/// </Summary>
/// <Param name = "str"> string to be checked </param>
/// <Returns> Chinese String Length </returns>
Public static int cnstringlength (string str)
{
Asciiencoding n = new asciiencoding ();
Byte [] B = n. getbytes (str );
Int l = 0; // l is the actual length of the string
For (int I = 0; I <= B. length-1; I ++)
{
If (B [I] = 63) // determines whether it is a Chinese character or full-legged symbol.
{
L ++;
}
}
Return l;
}
# Endregion
# Region: several characters on the right of the string
/// <Summary>
/// Take the characters on the right of the string
/// </Summary>
/// <Param name = "str"> string </param>
/// <Param name = "length"> several characters on the right </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 Replace the string on the right
/// <Summary>
/// Replace the string on the right
/// </Summary>
/// <Param name = "str"> string </param>
/// <Param name = "strsrc"> string on the right </param>
/// <Param name = "straim"> string to be replaced </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
}
}