Example 1
The code is as follows |
Copy Code |
private void Button2_Click (object sender, EventArgs e) { //Create a request for the URL. WebRequest request = WebRequest.Create ("http://www.111cn.net/"); //If required by the server, set the credentials. Request. Credentials = CredentialCache.DefaultCredentials; //Get the response. HttpWebResponse response = (HttpWebResponse) request. GetResponse (); //Display the status. MessageBox.Show (response. Statusdescription); Console.WriteLine (Response. Statusdescription); The //get the stream containing content returned by the server. Stream DataStream = response. GetResponseStream (); The //Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader (DataStream, Encoding.default); //Read the content. String responsefromserver = reader. ReadToEnd (); //intercepting data int i = responsefromserver.indexof ("Beijing"); String databid = responsefromserver.substring (i); Display the content. MessageBox.Show (Databid); Console.WriteLine (Responsefromserver); Cleanup the streams and the response. Reader. Close (); Datastream.close (); Response. Close (); } |
C # Obtains a text in the middle of a specified start and end string with a regular expression
The code is as follows |
Copy Code |
<summary> Gets the median value of the start and end strings in a string </summary> <param name= "str" ></param> <param name= "S" > Start </param> <param name= "E" > End </param> <returns></returns> public static string GetValue (String str, string s, String e) { Regex RG = new Regex (? <= ("+ S +")) [. \s\s]*? = ("+ E +")) ", Regexoptions.multiline | Regexoptions.singleline); Return RG. Match (str). Value; } <summary> Intercepting string too much + ... </summary> <param name= "InputString" > String </param> <param name= "len" > Length </param> <returns>string</returns> public static string Cutstring (string inputstring, int len) { ASCIIEncoding ASCII = new ASCIIEncoding (); int templen = 0; String tempstring = ""; Byte[] s = ASCII. GetBytes (inputstring); for (int i = 0; i < s.length; i++) { if ((int) s[i] = = 63) { Templen + 2; } Else { Templen + 1; } try { TempString + + inputstring.substring (i, 1); } Catch { Break } if (Templen > Len) Break } If it's truncated, add half an ellipsis. byte[] MyByte = System.Text.Encoding.Default.GetBytes (inputstring); if (MyByte. Length > Len) TempString + = "..."; return tempstring; } |
Call method
code is as follows |
copy code |
String Get string = "01-blue"; String Get after = Get string. Substring (0, get string. IndexOf ("-")); The gets the data that is: blue string get after = getting string. Substring (Get string. IndexOf ("-"), get string. Length-get string. IndexOf ("-")); |