. NET Framework win (next)

Source: Internet
Author: User
Tags first string

. NET Framework win (next)

(Www.jojoo.net) 2001-6-5 (double-click the automatic scrolling screen to watch it, click stop, and then click ..)

I. Processing strings
The. NET Framework class (or system class) provides a large number of core features that can be used to construct. NET applications. These features are applicable to any language environment. The first part of this article introduces basic concepts such as assembly and namespace, as well as the system. Math and system. Random classes. This is the second part of this article. Next we will discuss several other useful classes: system. String, system. array, system. datetime.

It is worth noting that, as mentioned above, when we use VB. net as a programming language, we often face the option of using VB. net Language built-in functions, or use the equivalent system class functions. We are faced with this option when processing array, date/time, and string data. If you used to be a VB 6.0 programmer, your first choice may be an proven and effective old method. However, if possible, you 'd better get rid of old habits and adopt the new. NET system class. Why? Because the use of the system class makes it easier for your code to be transplanted to other. NET languages and future VB. NET versions.

The system. string class provides rich string processing capabilities. Using the system. string class, we can: determine the length of the string, find the substring, change the case sensitivity of the string, compare two strings, split the string, and so on.

Determine whether the string length uses the Length attribute. For example, in the following code, the value of intlength is 4:

Dim strcolor as string = "blue"
Dim intlength as integer
Intlength = strcolor. Length

We use the indexof method to find the first matched substring from the string. If a substring can be found, the indexof method returns the starting position of the substring (the starting position of the first character is 0). If not, indexof returns-1. Indexof is case sensitive. Indexof is a overload method. The parameters that can be passed in include: Char-type characters, string-type strings, and char-type character arrays. The following indexof. ASPX page demonstrates the use of three different parameter types indexof methods:

<% @ Page Language = "VB" Explicit = "true" %>
<Head>
<Title> system. String instance </title>
<Script language = "VB" runat = "server">
Sub page_load (SRC as object, e as eventargs)
Dim chrg as char = "G"
Dim strword as string = ""
Dim chrvowels as char () = {"A", "E", "I", "O", "U "}
Dim strphrase as string = _
"One small step for man, one giant leap for mankind ."
Dim I as integer

Lbloutput. Text & = "<br/> strphrase =" & strphrase
Lbloutput. Text & = "<br/> position of chrg = "_
& Strphrase. indexof (chrg)
Lbloutput. Text & = "<br/> position of strword = "_
& Strphrase. indexof (strword)
Lbloutput. Text & = "<br/> position of chrvowels = "_
& Strphrase. indexof (chrvowels)
End sub
</SCRIPT>
</Head>
<Body>
<Asp: Label id = "lbloutput" runat = "server"/>
</Body>
</Html>

The running result of this page is as follows:

Indexof allows you to specify two optional parameters to restrict the search. They represent the start and end positions of the search string, respectively. For example, the following code restricts the chrvowels search to 10th to 20 characters:

Strphrase. indexof (chrvowels, 10, 20)

The lastindexof method is similar to the indexof method, but it searches for the last occurrence of a substring. For example, if you modify the indexof. ASPX page and use the lastindexof method to replace the indexof method, the location of strword will be 39 rather than 15.

The toupper and tolower methods of system. string can be used to change the strings to uppercase or lowercase letters respectively. For example:

Strupper = "this is a mixed case sentence". toupper ()
Strlower = "this is a mixed case sentence". tolower ()

From this example, we can see that the attributes and methods of system. string can be applied either in string variables or directly in string text.

You can use the compare method to compare whether two strings are the same. If the two strings are the same, the compare method returns 0. If the first string is smaller than the second string, compare returns a negative number. If the first string is greater than the second string, the compare method returns a positive number. Compare is a static method (see static methods and instance methods described earlier in this article ). By default, compare is case-sensitive for string comparison, regardless of the region relationship. For example, the following comparison of str1 and str2 will return-1, which indicates that str1 is smaller than str2:

Dim str1 as string = "abcd.com"
Dim str2 as string = "ABCD. com"

Answer = string. Compare (str1, str2)

We can pass in the third optional parameter to compare. If the third parameter is set to true, string comparison operations are case-insensitive. For example, the answer value in the following code is 0, that is, the two strings are equal.

Answer = string. Compare (str1, str2, true)

As with the indexof method, compare is also a method that is overloaded. We can pass in the fourth parameter to the compare method to compare the region. Alternatively, we can specify the start and end positions of characters so that the comparison is only a part of the string. For more information, see. NET Framework SDK documentation. The split method Splits a string into an array composed of substrings. When using the split method, we must specify the char-type delimiter used to separate strings. The following split. ASPX page demonstrates the application of the split method:

<% @ Page Language = "VB" Explicit = "true" %>
<Head>
<Title> split instance </title>
<Script language = "VB" runat = "server">
Sub page_load (SRC as object, e as eventargs)
Dim strasp as string = _
"ASP. NET is the next generation of Active Server Pages ."
Dim strwords () as string
Dim I as integer

Strwords = strasp. Split ("")

For I = strwords. getlowerbound (0) to strwords. getupperbound (0)
Lbloutput. Text & = I & ":" & strwords (I) & "<br/>"
Next
End sub
</SCRIPT>
</Head>
<Body>
<Asp: Label id = "lbloutput" runat = "server"/>
</Body>
</Html>

The output result of split. aspx is as follows:

We have discussed the application of some attributes and methods of the string class. String also contains many other Members, such as constructing a string from an array, replacing one character in the string with another character, and deleting the leading or trailing blank characters of the string.

Ii. Operation Array
We can use the system. array class to process arrays in various ways. Like the previous classes, many functions of the system. string class repeat the functions of the VB language. However, the array class also adds some functions not available in the traditional VB language, such as searching and sorting arrays.

The getlowerbound and getupperbound methods of the array class are used to determine the lower bound and upper bound of the specified dimension of the array. The following statement is from split. aspx (see the previous example). It uses the getlowerbound and getupperbound methods to determine the boundary of the strwords array:

For I = strwords. getlowerbound (0) to strwords. getupperbound (0)

The sort static method of system. array can sort the content of one-dimensional arrays. The sort method is case-sensitive for sorting arrays, and cannot sort arrays of more than one dimension. The syntax for calling the sort method is as follows:

Array. Sort (array_name)

For one-dimensional arrays, we can also use the reverse method to reverse the order of array elements. The syntax of the reverse method is similar to that of the sort method:

Array. Reverse (array_name)

The following code (from the arraysort. aspx Sample Page) demonstrates the application of the sort and reverse methods:

Dim strterms () as string = {"jscript", "VB", "asp", "ASP. NET", ". Net "}
Dim I as integer

Lbloutput. Text & = "original array <br/>"
For I = strterms. getlowerbound (0) to strterms. getupperbound (0)
Lbloutput. Text & = I & ":" & strterms (I) & "<br/>"
Next

Array. Sort (strterms)
Lbloutput. Text & = "<br/> after sorting <br/>"
For I = strterms. getlowerbound (0) to strterms. getupperbound (0)
Lbloutput. Text & = I & ":" & strterms (I) & "<br/>"
Next

Array. Reverse (strterms)
Lbloutput. Text & = "<br/> after reversing <br/>"
For I = strterms. getlowerbound (0) to strterms. getupperbound (0)
Lbloutput. Text & = I & ":" & strterms (I) & "<br/>"
Next

The output result of the arraysort. ASPX page is as follows:

The system. array method supports searching for one-dimensional arrays using the indexof and lastindexof methods. These two methods are similar to the methods of the same name in the system. string class. The syntax for searching Arrays Using the indexof and lastindexof methods is as follows:

Answer = array. indexof (array_name, search_string)
Answer = array. lastindexof (array_name, search_string)

The two methods return the first and last matched position of the search string respectively. If not, the return value is-1. This type of search is case sensitive. For example, in the following code, answer is 2, which indicates that the string "asp" is the third element of the strterms array.

Dim strterms () as string = {"jscript", "VB", "asp", "ASP. NET", ". Net "}
Answer = array. indexof (strterms, "asp ")

3. Process date/time data
The system. datetime class provides many methods to process datetime values. To create a datetime value, we only need to declare a datetime type variable and assign it a datetime constant through the "#" separator, as shown below:

Dim seattlequake as datetime = #02/28/01 #

A major advantage of the system. datetime class is that we can easily analyze the date/time value through its attributes. The meanings of these datetime attributes are obvious. They are: year, month, day, dayofweek, dayofyear, hour, minute, second, millisecond, ticks, and so on. Each ticks is equal to 100 nanoseconds ). For example, in the following code, the answer value is equal to 10:

Answer = seattlequake. Hour

We can also use the date and timeofday attributes to obtain the date or time part of datetime data. The timeofday attribute returns a timespan value, which indicates the elapsed time in ticks. As you can imagine, using the attribute of the timespan value, we can analyze each part of the timespan time. For more information, see. NET Framework SDK documentation.

The system. datetime class also provides several methods to increase (or decrease) a certain part of datetime values: addyears, addmonths, adddays, addhours, addminutes, addseconds, addmilliseconds, and addticks.

For example, the following code adds one year or minus one year to a specified date (bday:

Dim bday as datetime = #6/25/2001 12:00 #
Dim nextbday as datetime
Dim lastbday as datetime

Nextbday = thedate. addyears (1)
Lastbday = thedate. addyears (-1)

[Reference]

This section describes the history of COM, the. NET Framework, and the relationship between the two: http://www.microsoft.com/net/#/framework_com.asp.
Net Framework developer resources: http://msdn.microsoft.com/net/framework/default.asp

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.