1024programmer Asp.Net C# string processing

C# string processing

C# string processing

String processing

Case conversion

ToUpper and ToLower methods

string str="ASDFGHhjkl";
 str.ToUpper()//Convert all to uppercase
 str.ToLower();//Convert all to lowercase
 

Convert letters to ASCII code

//Convert letters to ASCII
 string str="A";


 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
 Encoding encoding = Encoding.GetEncoding("unicode");
 byte [] by=encoding.GetBytes(str);
 string ASCII=by[0].ToString();
 //ASCII to letters
 int x;
 string lettr=((char)x).ToString();
 

Encoding n.[calculation] encoding v. [calculation] encoding (ing form of encode)

The audio tag is not compatible with the browser you are using.

Apps running .NET Core or .NET 5+, these versions of .NET do not include all encoding. You need to register the System.Text.Encoding.CodePages package and then use the Encoding.GetEncoding method.

//Register System.Text.Encoding.CodePages package
 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
 

Chinese characters to location code

Convert Chinese characters to location code

public string GetCode(string Chinese)
 {
     ////Register System.Text.Encoding.CodePages package
     Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
     Encoding gb2312 = Encoding.GetEncoding("GB2312");
     byte[] bytes = gb2312.GetBytes(Chinese);
     //Extract location code
     int front = bytes[0] - 160;
     int back = bytes[1] - 160;

     return front.ToString() + back.ToString();
 }
 

Convert location code to Chinese characters

public string GetChinese(string code)
 {
     ////Register System.Text.Encoding.CodePages package
     Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
     Encoding gb2312 = Encoding.GetEncoding("GB2312");
     int front = Convert.ToInt32(code.Substring(0, 2))+160;
     int back = Convert.ToInt32(code.Substring(2, 2))+160;
     byte[] bytes = new byte[] { (byte)front, (byte)back };

     return gb2312.GetString(bytes);
 }
 

A Substring() function is used here, the usage is as follows

Substring() function

string str = "Hello, World!";
 string sub1 = str.Substring(7); // Return "World!"
 string sub2 = str.Substring(7, 5); // Return "World"
 string sub2 = str.Substring(7, 4); //Return "Worl"
 

string Substring(int startIndex): This form returns a new string starting from the specified index of the current string to the end. Indexing is 0-based, so if startIndex is 0, then it will return the original string.

string Substring(int startIndex, int length): This form returns a new string that starts at the specified index of the current string and is of the specified length. If startIndex is 0, then it returns a string of length length whose contents are the same as the first length characters of the original string.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811178

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索