. NET string, and how the regular expression is used in the

Source: Internet
Author: User
Tags date format expression numeric value split string format tostring trim
Regular | string

No matter what type of application you create, you need to use a string. Regardless of how the data is stored, end users always have to deal with readable text. So, knowing how to use strings is any. NET developers need to learn the necessary knowledge to create a rich application. Except to show you the. NET Framework, this article will also introduce you to the knowledge of regular expressions.

In addition to showing you how to use. NET Framework, this article will also introduce you to regular expressions. Regular expressions are format codes, not only does it allow you to verify that a particular string matches a given format, but you can also use regular expressions to extract meaningful information from any other text that might be considered free-form, such as extracting the first name from the user's input, or extracting the code from a numeric input, Or extract the server name from a URL.

   The use of a string

Using strings is a necessary technique for creating high-quality applications. Even if you are working with digital or image data, end users need contextual feedback. This article will introduce to you. NET strings, how to format them, manipulate them and compare them, and other useful operations.

(i). NET string Introduction

Before the. NET Framework and the common language runtime (CLR), developers always spend a lot of time processing strings. The reusable Library of a string routine is almost part of every C and C + + Programmer's Toolbox. Writing code to Exchange string data between different programming languages is also quite difficult. For example, Pascal stores a string as an array of memory characters, where the first element of the array indicates the length of the string, while C stores the string as a variable-length array of character memory with an ASCII null character at the end of the string, represented by "" in C.

In the. NET Framework, strings are stored in a constant fashion. This means that when you use C # (or whatever else.) NET language) when a string is created, the string is stored in memory at a fixed size so that the CLR can run faster. The result is that when you implement a single character such as a connection string or modify a string, the CLR is actually creating multiple copies of your string.

Strings in C # are the same as other value types, such as integers or floating-point number declarations, as shown in the following example:

string x = "Hello World";
String y;
string z = x;
(ii) format string

The most common task when using strings is to format strings. When displaying information to the user, you often display such things as date, time, numeric value, decimal value, currency value, or even hexadecimal digits. C # strings can display these types of information, even more. Another powerful feature is that when you use the standard Formatting tool, the formatted output has a locale-aware feature. For example, if you display the current date of an English user in a short format, the short format of the current date will be displayed in a different form for an American user.

To create a formatted string, you only need to call the string class's format method and pass it a format string, as shown in the following code:

String formatted = string. Format ("The value is {0}", value);
Here, the {0} placeholder indicates where a value should be inserted. In addition to specifying where a value should be inserted, you can also specify the format of the value.

Other data types also support the conversion of a custom format modifier to a string, such as a datetime data type, which produces a custom-formatted output by using the following methods:

DateTime.ToString ("format specifiers");
Table 1 lists some of the most commonly used format strings for formatting dates, times, numeric values, and so on.

Table 1. Custom DateTime format Modifiers

Modifiers Describe
D Displays the day of the month.
Dd Displays the day of the month, where the value less than 10 is preceded by a 0.
Ddd Displays the three-letter abbreviation for one day of the one week.
dddd (+) Displays the full name of the day of the week in the given datetime value.
F (+) Displays the most important number of x digits for the second value. The greater the number of format modifiers in F, the more important the numbers. This is the total number of seconds, not the number of seconds that have elapsed since the last minute.
F (+) The same as F (+), except that the end of 0 is not displayed.
G Displays the era in a given datetime, for example, "A.D".
H Display hours, Range: 1~12.
hh Display hours, Range: 1~12, where the value of less than 10 plus a 0.
H Displays the hour range as: 0~23.
HH Displays the hour range as: 0~23, where the value less than 10 is preceded by a 0.
M Displays minutes with a range of 0~59.
Mm Displays minutes with a range of 0~59, where the value of less than 10 is preceded by a 0.
M Displays the month, the range is 1~12.
Mm Displays the month, the range is 1~12, where the value less than 10 is preceded by a 0.
MMM Displays the three-character abbreviation for the month.
MMMM Displays the full name of the month.
S Displays the number of seconds range: 0~59.
SS (+) The number of seconds displayed is: 0~59, where a value of less than 10 is preceded by a 0.
T Displays the first character in the AM/PM indicator for a given time.
TT (+) Displays the complete AM/PM indicator for a given time.
Y/yy/yyyy Displays the year in a given time.
Z/zz/zzz (+) Displays the time zone offset for the given period.

Let's look at the following code, which shows a date and time string that uses the string format modifier to create a custom format:

DateTime dt = DateTime.Now;
Console.WriteLine (String. Format ("Default format: {0}", dt.) ToString ()));
Console.WriteLine (dt. ToString ("dddd DD MMMM, yyyy g"));
Console.WriteLine (String. Format ("Custom format 1: {0:mm/dd/yy hh:mm:sstt}", DT));
Console.WriteLine (String. Format ("Custom format 2: {0:HH:MM:SSTT g\\mt zz}", DT));
The following is the output from the preceding code:

Default format:9/24/2005 12:59:49 PM
Saturday September, A.D.
Custom Format 1:09/24/05 12:59:49pm
Custom Format 2:12:59:49pm GMT-06
You can also provide custom format modifiers for numeric values. Table 2 describes the custom format modifiers that apply to numeric values.

Table 2. Digital Custom Format modifiers

Modifiers Describe
0 A zero-placeholder character.
# Digit placeholder. If there is a number in the given value at the position indicated by the # modifier, the number is displayed in the formatted output.
. decimal point.
The thousand-bit separator.
% Percent modifier. The formatted value is multiplied by 100 before being included in the formatted output.
e0/e+0/e/e+0/e-0/e Scientific signs.
"XX" or "XX" A string representing the format. These are included in the formatted output without translating its relative position.
; A section separator that is used for conditional formatting of negative numbers, 0, and positive values.

If you define multiple format sections, you can control the formatting of numbers more precisely:

· Two sections-if you have two formatting sections, the first section applies to all positive numbers (including 0) values. The second section applies to negative values, which are very handy when you want to include negative values in parentheses (as in many financial packages).

· Three sections-If you have three formatting sections, the first section applies to all positive numbers (excluding 0) values. The second section applies to negative values, and the third section applies to zeros.

The following code shows how to use a custom number format modifier.

Double dval = 59.99;
Double Dneg =-569.99;
Double zeroval = 0.0;
Double pct = 0.23;
String formatstring = "{0:$#,## #0; ($#,## #0.); Nuttin}";
Console.WriteLine (String. Format (formatstring, dval));
Console.WriteLine (String. Format (formatstring, Dneg));
Console.WriteLine (String. Format (formatstring, zeroval));
Console.WriteLine (Pct. ToString ("0%"));
The preceding code will produce the output shown below:

$59.99
($569.99)
Nuttin
23%

(iii) manipulating and comparing strings

In addition to displaying strings containing various formatted data, other common string-related tasks are string manipulation and comparisons. One of the important things to remember is that the string is actually. NET Framework, a class in the base Class library. Because it's a class, you can actually call a string of methods, just as you can call methods on any other class.

You can call these methods in string constants or string variables, see the following code:

int x = string. Length ();
int y = "Hello world". Length ();
Table 3 briefly enumerates some of the most commonly used methods you can use for strings to get information about the string or manipulate it.

Table 3. Common String Instance methods

Method Describe
CompareTo Compare this string instance with other string instances.
Contains Returns a Boolean value indicating whether the current string instance contains the given substring.
CopyTo Copies a substring from a string instance to a specific position in a character array.
EndsWith Returns a Boolean value indicating whether the string ends with a given substring.
Equals Indicates whether the string is equal to another string. You can also use the ' = = ' operator instead.
IndexOf Returns the index of a substring in a string instance.
IndexOfAny Returns the occurrence of the first index of any character within a substring in a string instance.
PadLeft Fills a string with a specific number of spaces or other Unicode characters, especially for string-right alignment.
PadRight Adds a specific set of space characters or other Unicode characters to the end of the string, creating a string right alignment effect.
Remove Deletes a given number of characters from a string.
Replace Replaces all occurrences of a given character or string in a string instance with a specific substitute.
Split Breaks the current string into an array of strings using a specific character as the dividing point.
StartsWith Returns a Boolean value that indicates whether the string instance starts with a specific string.
SubString Returns a specific portion of a string, given the starting point and length.
ToCharArray Converts a string into an array of characters.
ToLower Convert all strings to lowercase characters.
ToUpper Converts the string into uppercase characters.
Trim Deletes all occurrences of a given set of characters from the start and end positions of a string.
TrimStart Implements the Trim function, but only from the beginning of the string.
TrimEnd Implements trim functionality, but only from the end of the string.

The following code shows that you can use the corresponding function above to implement string queries and operations, and so on:

String sourcestring = "Mary Had a Little Lamb";
String sourceString2 = "Mary Had a Little Lamb";
Console.WriteLine (Sourcestring.tolower ());
Console.WriteLine (String. Format ("The string ' {0} ' is {1} chars long",
Sourcestring,sourcestring.length));
Console.WriteLine (String. Format ("Fourth word in sentence is: {0}",
Sourcestring.split (") [3]));
Console.WriteLine (Sourcestring2.trim ());
Console.WriteLine ("Two Strings equal" + (sourcestring = Sourcestring2.trim ()));
The preceding code output shows the following:

Mary had a little lamb
The string ' Mary Had a Little Lamb ' is chars long.
Fourth word in sentence is:little
Mary Had a Little Lamb
Two strings equal? True
(iv) Introduction to StringBuilder

As mentioned earlier, strings are constants. This means that when you concatenate two strings into a new string, there is a time when the CLR has three strings in memory. So, for example, when you connect to implement the connection shown in the following code:

String a = "Hello";
String B = "World";
String c = one + "" + C;
In fact, there are four strings in memory, including spaces. To ease this string connection performance problem and provide you with a tool to make the connection easier. NET Framework provides a class StringBuilder.

By using StringBuilder to dynamically create variable-length strings, you overcome the fact that a constant string is in a CLR string, and, as such, the code has become more readable. The following code shows the use of StringBuilder:

StringBuilder sb = new StringBuilder ();
Sb. Append ("greetings!\n");
formatstring = "{0:$#,## #0; ($#,## #0.00); Zero} ";
Dval = 129.99;
Sb. AppendFormat (formatstring, dval);
Sb. Append ("\nthis is a big concatenated string.");
Console.WriteLine (sb.) ToString ());
The preceding code output shows the following:

greetings!
$129.99
This is a big concatenated string.
Note that the "\ n" in the preceding code inserts a newline character into the string.

   ii. use of regular expressions

Regular expressions allow for quick and efficient processing of text. The processed text is small to an email address, large to a multiline input box content. The use of regular expressions not only allows you to use a defined pattern to validate text, but also allows you to extract data from text that matches a given pattern.

You can think of a regular expression as a particularly powerful wildcard. When we see expressions like "sams*", we are familiar with wildcard characters, and any content that starts with the word SAMS is a matching expression. Regular expressions can provide you with a powerful control function that far exceeds the wildcard character.

This section will give you a brief introduction. NET Framework provides classes that support the use of regular expressions. For more information about regular expressions, you can refer to the regular Expression Quick reference manual or the second edition of proficiency in regular expressions. These books will provide you with the information you need to create your own regular expressions, and also provide a list of common regular expressions. As for the formal expression itself, it is beyond the scope of this article.

(i) Validation input

One of the most common places for regular expressions is to validate user input by using predefined formats (for example, enforcing rules to ensure that passwords contain specific characters that make them difficult to break). These rules are typically defined as regular expressions. Regular expressions are also used to validate simple input, such as e-mail addresses and phone numbers.

. NET Framework is the Regex class, a key class for operating regular expressions. This class provides a static method IsMatch that returns a Boolean value indicating whether the specified input string matches a given regular expression.

In the following code, use a normal regular expression to test the validity of an e-mail address:

String Emailpattern = @ "^" ([\w-\.] +) @ (\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | [CCC]
(([\w-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?) $";
Console.Write ("Enter an e-mail address:");
String emailinput = Console.ReadLine ();
BOOL match = Regex.IsMatch (Emailinput, Emailpattern);
if (match)
Console.WriteLine ("E-mail address is valid.");
Else
Console.WriteLine ("Supplied input is not a valid e-mail address.");
If you don't know the formal expression, don't worry. The basic idea of e-mail mode is that it requires some alphanumeric characters, followed by an @ sign, then a combination of characters followed by a "." followed by at least two characters. You can take a look at the previous code in different input tests to see what you get. Even if you do not understand the normal expression itself, as long as you know their existence, then you can use it in your application to validate input.

(ii) Extraction of data from input

Other common uses of regular expressions are to parse text based on an expression and use it to extract data from user input (called a group match).

A formal expression includes a feature called a group. A group allows you to place a named identity in a particular section of the regular expression. When you call match () to enter data for a pattern comparison, the results actually divide the match into groups that allow you to extract the parts that match the input of each group.

For example, in the previous example, we created a username that allows us to extract all the data that precedes the @ symbol in an e-mail address. Then, when a match is performed, we are able to extract the username from the input using a named group of regular expressions.

The following code shows how to extract the protocol name and port number from a user-entered URL in the console. The greatness of regular expressions is that they use their own language; therefore, they do not have to rely on C, C + +, C #, VB. NET or any other language. The regular expression in the following code comes from an MSDN example:

String Urlpattern = @ "^ (? <proto>\w+)://[^/]+?" (? <port>:\d+)?/";
Console.WriteLine ();
Console.Write ("Enter a URL for data parsing:");
String url = Console.ReadLine ();
Regex urlexpression = new Regex (Urlpattern, regexoptions.compiled);
Match urlmatch = urlexpression.match (URL);
Console.WriteLine ("The Protocol you entered is" + urlmatch.groups["Proto"]. Value);
Console.WriteLine ("The port number you entered is" + urlmatch.groups["Port"]. Value);
When you run the preceding code with a URL without a port number, you notice that you don't get any group values. This is because the input does not match the regular expression at all. When there is no match, it is obvious that you cannot extract meaningful data from a given group. When you run the preceding code using a URL that matches the port number of the regular expression, you will get the output as shown in the following text:

Entera URL for Data parsing:http://server.com:2100/home.aspx
The Protocol you entered is HTTP
The Port number you entered was:2100
   Third, summary

In this article, you have seen now that you have your own string example threading. With the help of C # and the. NET Framework, strings become a native part of the base Class library, and provide you with a large number of tool methods to implement string comparisons, operations, formatting, and so on. You also see that the StringBuilder class provides you with a set of Easy-to-use tool methods to dynamically build strings without the performance penalty of local string concatenation.

Finally, this article gives you a brief introduction to the power of regular expressions and how the Regex class integrates these functions together. By reading this article and experimenting with the appropriate sample code, you should be familiar with string and regular expression operations to make your application more powerful.

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.