C # some practical functions

Source: Internet
Author: User
Tags servervariables

Obtain the real IP address of the remote user through the proxy server:
If (request. servervariables ["http_via"]! = NULL ){
String user_ip = request. servervariables ["http_x_forwarded_for"]. tostring ();
} Else {
String user_ip = request. servervariables ["remote_addr"]. tostring ();
}
System. Text. encoding. Default. getbytes (variable)
Convert character code to bit code
For example, byte [] bytstr = system. Text. encoding. Default. getbytes (STR );

7. Variable. substring (parameter 1, parameter 2 );
Part of the string to be truncated. Parameter 1 is the start number of digits on the left, and parameter 2 is the number of digits to be truncated.
For example, string S1 = Str. substring );

16. Char. iswhitespce (STRING variable, digits) -- Logical type
Check whether the specified position contains null characters;
17. Char. ispunctuation (''' character ''') -- Logical type
Check whether a character is a punctuation mark.
For example: Response. Write (char. ispunctuation ('''a' ''''); // return: false
18. (INT) ''' character ''''
Convert the character into a number and check the code. Note that it is a single quotation mark.
For example:
Response. Write (INT) ''' in '''); // The code with the result of the medium character: 20013

19. (char) Code
Convert the number into characters and check the characters represented by the Code.
For example:
Response. Write (char) 22269); // return the word "country.

21. string variable. Replace ("substring", "Replace ")
String replacement
For example:
String STR = "China ";
STR = Str. Replace ("country", "Central"); // Replace the Chinese character with the central character
Response. Write (STR); // The output result is "Central"

Another example is: (this is very practical)

String STR = "This Is A <SCRIPT> script ";
STR = Str. replace ("<", "<font> </font> "); // Replace the left angle brackets with <font> and </font> (or replace them with <, but it is estimated that after saving the XML, it will be restored again)
Response. Write (STR); // display as: "This is a <SCRIPT> script"

If not replaced, <SCRIPT> is not displayed. If it is a script, it runs. If it is replaced, the script does not run.
The value of this Code is that you can invalidate and display all HTML tags in a text file to protect your interactive websites.
Specific implementation: Add the following code to your form submission button script:
String strsubmit = label1.text; // label1 is the Control ID for you to submit data.
Strsubmit = strsubmit. Replace ("<", "<font> </font> ");
Then save or output strsubmit.
This method can also be used to easily implement the UBB code.
23. String comparison is generally used: If (str1 = str2) {}, but there are other methods:

(1 ),
String str1; str2
// Syntax: str1.endswith (str2); _ check whether str1 ends with str2 and returns a Boolean value. For example:
If (str1.endswith (str2) {response. Write ("string str1 ends with" + str2 + ");}

(2 ),
// Syntax: str1.equals (str2); _ check whether str1 is equal to str2. Return a Boolean value, which is the same as the preceding syntax.

(3 ),
// Syntax equals (str1, str2); _ checks whether str1 is equal to str2 and returns a Boolean value.

24. indexof (), lastindexof ()
Search for the position where the specified character or string appears for the first time (the last time) in the string, and return the index value, for example:
Str1.indexof ("word"); // search for the index value (location) of the word in str1)
Str1.indexof ("string"); // search for the index value (location) of the first character of "string" in str1)
Str1.indexof ("string", 4th); // query the index value (location) of the first character of the "string" in str1 for 2 characters starting from str1 characters)

25. insert ()
Insert the specified character into the specified index position in the string. For example:
Str1.insert (1, "word"); Insert "word" to the second character of str1. If str1 = "China", it is inserted as "Chinese country ";

26. padleft (), padright ()
Add a space or a specified char character to the left (or right) of the string so that the string reaches the specified length, for example:
<%
String str1 = "Chinese ";
Str1 = str1.padleft (10, '''1'); // if no second parameter is specified, a space is added.
Response. Write (str1); // The result is "1111111 Chinese" and the string length is 10.
%>
27. Remove ()
Deletes a specified number of characters from a specified position.
String comparison is generally used: If (str1 = str2) {}, but there are other methods:

1,
String str1; str2
// Syntax: str1.endswith (str2); _ check whether str1 ends with str2 and returns a Boolean value. For example:
If (str1.endswith (str2) {response. Write ("string str1 ends with" + str2 + ");}

2,
// Syntax: str1.equals (str2); _ check whether str1 is equal to str2. Return a Boolean value, which is the same as the preceding syntax.

3,
// Syntax equals (str1, str2); _ checks whether str1 is equal to str2 and returns a Boolean value.

Indexof ()
Searches for the position of the specified character or string that appears for the first time in the string, and returns the first index value, for example:
Str1.indexof ("word"); // search for the index value (location) of the word in str1)
Str1.indexof ("string"); // search for the index value (location) of the first character of "string" in str1)
Str1.indexof ("string", 4th); // query the index value (location) of the first character of the "string" in str1 for 2 characters starting from str1 characters)

1. datetime numeric type
System. datetime currenttime = new system. datetime ();
1.1 take the current year, month, day, hour, minute, second
Currenttime = system. datetime. now;
1.2 get current year
Int year = currenttime. Year;
1.3 take the current month
Int month = currenttime. month;
1.4 get the current day
Int day = currenttime. Day;
1.5 current time
Int = currenttime. hour;
1.6 get the current score
Int = currenttime. minute;
1.7 takes the current second
Int second = currenttime. Second;
1.8 takes the current millisecond
Int millisecond = currenttime. millisecond;
(The variable can be in Chinese)

1.9 display Chinese date -- year, month, and day
String stry = currenttime. tostring ("F"); // seconds not displayed

1.10 use Chinese date to display _ year and month
String strym = currenttime. tostring ("Y ");

1.11 retrieve Chinese Date display _ month/day
String strmd = currenttime. tostring ("M ");

1.12 take the current year, month, and day in the format of 2003-9-23
String strymd = currenttime. tostring ("D ");

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.