search
HomeBackend DevelopmentC#.Net TutorialA collection of commonly used small functions and methods in c#.net

1. DateTime numeric type System.DateTime currentTime=new System.DateTime(); 1.1 Get the current year, month, day, hour, minute and second currentTime=System.DateTime.Now; 1.2 Get the current year int year = currentTime.Year; 1.3 Get the current month int Month = currentTime.Month; 1.4 Get the current day int day = currentTime.Day; 1.5 Get the current time int hour = currentTime.Hour; 1.6 Get the current minute int minute = currentTime.Minute; 1.7 Get the current second int second = currentTime.Second; 1.8 Get the current millisecond int millisecond = currentTime.Millisecond; (variables are available in Chinese) 2. Int32.Parse (variable) Int32.Parse ("constant") character type is converted into a 32-bit digital type 3. Variable.ToString() character type Convert to string 12345.ToString("n");//Generate 12,345.0012345.ToString("C");//Generate ¥12,345.0012345.ToString("e");//Generate 1.234500e+00412345.ToString(" f4");//Generate 12345.000012345.ToString("x");//Generate 3039 (hexadecimal) 12345.ToString("p");//Generate 1,234,500.00%4, variable .Length digital type takes the string length : Such as: string str="China";int Len = str.Length;//Len is a custom variable, str is the variable name of the string to be tested 5. System.Text.Encoding.Default.GetBytes (variable) word Convert the code to bit code such as: byte[] bytStr = System.Text.Encoding.Default.GetBytes(str); Then you can get the bit length: len = bytStr.Length; 6. System.Text.StringBuilder("") String addition, (is the same with + sign?) For example: System.Text.StringBuilder sb = new System.Text.StringBuilder("");sb.Append("China");sb.Append("People" );sb.Append("Republic");7. Variable.Substring(parameter 1, parameter 2); intercept part of the string, parameter 1 is the starting digit from the left, and parameter 2 is the number of digits to intercept. For example: string s1 = str.Substring(0,2); 8. String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString(); Get the remote user's IP address 9. Get the remote user's real IP address through the proxy server: if(Request.ServerVariables["HTTP_VIA"]!=null){stringuser_IP=Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();}else{stringuser_IP=Request.ServerVariables["REMOTE_ADDR"].ToString();}10 , Session["variable"]; access Session value; for example, assignment: Session["username"]="Bush"; value: Object objName=Session["username"];String strName=objName.ToString() ; Clear: Session.RemoveAll(); 11. String str=Request.QueryString["Variable"]; Use hyperlink to transfer variables. For example, create a hyperlink in any page: Click to get the value in the Edit.aspx page: String str=Request.QueryString["fdid"];12 , DOC object.CreateElement("New node name"); Create a new node of the XML document 13. Parent node.AppendChild (child node); Add the newly created child node to the parent node of the XML document 14. Parent node.RemoveChild (node) ;Delete node 15, ResponseResponse.Write("string");Response.Write(variable);Output to the page. Response.Redirect("URL address"); jump to the page specified by the URL 16. char.IsWhiteSpce (string variable, number of digits) - logical type to check whether the specified position is a null character; such as: string str="Chinese People" ;Response.Write(char.IsWhiteSpace(str,2)); //The result is: True, the first character is 0, and 2 is the third character. 17. char.IsPunctuation('Character')--Logical check whether the character is a punctuation mark, such as: Response.Write(char.IsPunctuation('A'));//Return: False18, (int)'Character' returns the character Convert to a number, look up the code point, pay attention to the single quotes. For example: Response.Write((int)'中');//The result is the Chinese character code: 2001319. The (char) code converts numbers into characters and checks the characters represented by the code. For example: Response.Write((char)22269);//Return the character "国". 20. Trim() clears the spaces before and after the string 21. String variable.Replace("substring", "replace with") string replacement such as: string str="China";str=str.Replace("国" ,"central");//Replace the national character with the central character Response.Write(str);//The output result is "central" Another example: (This is very practical) string str="This is <script>script"; str=str.Replace("<","<font><");//Replace the left angle bracket with <font> and < with (or replace is &lt, but it is estimated that it will be restored after being saved in XML and submitted again) Response.Write(str); //Displayed as: "This is a <script> script" If not replaced, <script> will not be displayed. If it is a script, it will run; but after replacement, the script will not run. The value of this code is that you can invalidate all HTML tags in a text, display them all, and protect your interactive site. Specific implementation: Add the following code to your form submit button script: string strSubmit=label1.Text;//label1 is the control ID that allows users to submit data. strSubmit=strSubmit.Replace("<","<font><");Then save or output strSubmit. UBB code can also be easily implemented using this method. 22. Math.Max(i,j) takes the maximum value between i and j, such as int x=Math.Max(5,10); // x will take the value 10 </script>

23. String comparison is generally used: if(str1==str2){ }, but there are other methods:

(1),

string str1; str2

//Syntax: str1.EndsWith(str2 ); __Detect whether the string str1 ends with the string str2 and return a Boolean value. For example:

if(str1.EndsWith(str2)){ Response.Write("String str1 ends with "+str2+"" ); }

(2),

//Syntax: str1.Equals(str2); __Check whether the string str1 is equal to the string str2 and return a Boolean value. The usage is the same as above. //Syntax Equals(str1, str2); __Check whether the string str1 is equal to the string str2 and return a Boolean value. The usage is the same as above.

24, IndexOf(), LastIndexOf()

Find the specified character in the string or The position where the string appears for the first time (last time) returns the index value, such as:

str1.IndexOf("word"); //Find the index value (position) of "word" in str1

str1.IndexOf(" String"); //Find the index value (position) of the first character of "string" in str1

str1.IndexOf("string",3,2); //From the 4th character of str1 Starting from 2 characters, find the index value (position) of the first character of "string" in str1

25, Insert()

Insert the specified character at the specified index in the string. For example:

str1.Insert(1,"字"); Insert "字" at the second character of str1. If str1="China", the inserted character will be "中文国";

26, PadLeft() , PadRight()

Add a space or specify a char character to the left (or right) of the string to make the string reach the specified length, such as:

string str1="Chinese";

str1=str1 .PadLeft(10,''''1''''); //If there is no second parameter, add a space

Response.Write(str1); //The result is "1111111 Chinese", the string length is 10

%>

27, Remove()

Delete the specified number of characters starting from the specified position

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the alternatives to NULL in C languageWhat are the alternatives to NULL in C languageMar 03, 2025 pm 05:37 PM

This article explores the challenges of NULL pointer dereferences in C. It argues that the problem isn't NULL itself, but its misuse. The article details best practices for preventing dereferences, including pre-dereference checks, pointer initiali

How to add next-level C compilerHow to add next-level C compilerMar 03, 2025 pm 05:44 PM

This article explains how to create newline characters in C using the \n escape sequence within printf and puts functions. It details the functionality and provides code examples demonstrating its use for line breaks in output.

Which C language compiler is better?Which C language compiler is better?Mar 03, 2025 pm 05:39 PM

This article guides beginners on choosing a C compiler. It argues that GCC, due to its ease of use, wide availability, and extensive resources, is best for beginners. However, it also compares GCC, Clang, MSVC, and TCC, highlighting their differenc

Is NULL still important in modern programming in C language?Is NULL still important in modern programming in C language?Mar 03, 2025 pm 05:35 PM

This article emphasizes the continued importance of NULL in modern C programming. Despite advancements, NULL remains crucial for explicit pointer management, preventing segmentation faults by marking the absence of a valid memory address. Best prac

What are the web versions of C language compilers?What are the web versions of C language compilers?Mar 03, 2025 pm 05:42 PM

This article reviews online C compilers for beginners, focusing on ease of use and debugging capabilities. OnlineGDB and Repl.it are highlighted for their user-friendly interfaces and helpful debugging tools. Other options like Programiz and Compil

Method of copying code by C language compilerMethod of copying code by C language compilerMar 03, 2025 pm 05:43 PM

This article discusses efficient code copying in C IDEs. It emphasizes that copying is an IDE function, not a compiler feature, and details strategies for improved efficiency, including using IDE selection tools, code folding, search/replace, templa

C language online programming website C language compiler official website summaryC language online programming website C language compiler official website summaryMar 03, 2025 pm 05:41 PM

This article compares online C programming platforms, highlighting differences in features like debugging tools, IDE functionality, standard compliance, and memory/execution limits. It argues that the "best" platform depends on user needs,

How to solve the problem of not popping up the output window by the C language compilerHow to solve the problem of not popping up the output window by the C language compilerMar 03, 2025 pm 05:40 PM

This article troubleshoots missing output windows in C program compilation. It examines causes like failing to run the executable, program errors, incorrect compiler settings, background processes, and rapid program termination. Solutions involve ch

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment