(5) Continuous in-depth C #

Source: Internet
Author: User

This lesson learned a wide range of regular expressions, and it also played a good role in C #, and here's an example.

We first create a regular instance and then use it to process a string, APPENDFORMT is the method that handles the compound format string, and the returned string is appended to this instance.

This is a matching string and returns the length of the string. It embodies the basic function of regular expression. Looked up some information and learned some of the relevant knowledge of regular expressions:

When you new a Regex object in C #, the second parameter can support the selection of matching patterns with enumerations, and now the effect of these enumeration values on the regular.

Mode description

. Singleline Dot can match any character
. Multiline extension ^ and $ match so that ^ and $ can match line breaks inside a string
. Ignorepatternwhitespace design loose arrangement and annotation mode
. IgnoreCase a case-insensitive match
. ECMAScript restricts \w \s \d, making it valid only for ASCII characters
. The driving process of the RightToLeft drive is constant, but in the opposite direction (starting at the end of the character and moving toward the beginning)
. Compiled spend more time optimizing regular expressions, compiling into DLLs, and consuming multiple points of memory, but matching is faster.
. Explicitcapture normal brackets () are normally captured in parentheses, but in this mode with (?: ...) Same, grouped, not captured

The Regex has two methods of match (), Matches () where a groups in the match () method can contain all the group found by match.

string string1 = "04:03:27 127.0.0.0 libertyassociates.com" +
"04:03:28 127.0.0.0 foo.com" + "04:03:29 127.0.0.0 bar.com";
Group time = one or more digits or colons followed by space
Group IP address = one or more digits or dots followed by space
Group site = One or more characters, \s:any none space characters
Regex thereg = new Regex (@ "(?<time> (\d|\:) +) \s"
[Email protected] "(?<ip> (\d|\.) +) \s "+ @" (? <site>\s+) ");
Get the collection of matches
MatchCollection thematches = thereg.matches (string1);
foreach (Match thematch in thematches)
{//Iterate through the collection
if (thematch.length! = 0)
{
Console.WriteLine ("\nthematch: {0}", thematch.tostring ());
Console.WriteLine ("Time: {0}", thematch.groups["Time"]);
Console.WriteLine ("IP: {0}", thematch.groups["IP"]);
Console.WriteLine ("Site: {0}", thematch.groups["site"]);
}
}

This method can be used to separate the parts of the string, solving the problem that sometimes requires importing multiple pieces of information at the same time.

Now let's explore the issue of using the Windows Asynchronous API, where we use two keywords: async, await

Public async task<int> Examplemethodasync ()
{
var httpClient = new HttpClient ();
int exampleint = (await
Httpclient.getstringasync ("http://msdn.microsoft.com")). Length;

Resultstextbox.text + = "Preparing to finish examplemethodasync.\n";
return exampleint;
}
The await keyword used here represents the invocation of the asynchronous interface and waits for the result to be returned before the subsequent code executes. This function uses a keyword in its definition: async, because there is at least one place in the function where the await keyword is used, then this function must be defined as async. You can also use the AWAIT keyword when calling this function outside, for example:

BOOL result = await examplemethodasync ();

Similarly, the function in which this line of code is located must also declare the ASYNC keyword. In fact, when invoking the asynchronous API, you can also not write these two keywords, but the Code execution order will change, the return result type has changed.

Today, I'd like to introduce you to something about data type conversions: Int.parse (), Int. Tryparse () and Convert.ToInt32 (). They have similar effects, but they are very different. Convert.ToInt32 is suitable for converting the object class type to an int type, such as Convert.ToInt32 (session["Shuzi"]); Int. Parse is suitable for converting a string class type to an int type, such as Int. Parse (session["Shuzi"). ToString ()),

Int. TryParse (input, out i)

The first parameter is the value to be converted, the second parameter is the output value, if the conversion fails, the output value is 0, if the conversion succeeds, the output value is the converted int value

About Int. TryParse (input, out i), the expression conversion succeeds returns True, and the conversion failure returns false.

For the floating-point problem, floating-point type only Convert.ToInt32 () and (int) can be converted, but it is also a trade-off, Convert.ToInt32 () take the trade-offs are rounding, and (int) is to intercept the integer portion of the floating-point type, Ignoring fractional portions, such as Convert.ToInt32 (1.499d) and (int) 1.499d, returns 1,convert.toint32 (1.5D) and returns 2, while (int) 1.5d returns 1;

Convert.ToInt32 (double value) if value is a number in the middle of a two integer, it returns the even of the two, that is, 3.5 is converted to 4, 4.5 is converted to 4, and 5.5 is converted to 6. But 4.6 can be converted to 5, 4.4 to 4

A little bit of learning experience, there are mistakes please correct me.

(5) Continuous in-depth C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.