C # Application -- Simplified Chinese to traditional Chinese

Source: Internet
Author: User

C # Application -- Simplified Chinese to traditional Chinese

The following describes how to use the c # string and Hashtable class to convert simplified Chinese characters to traditional Chinese characters, and then analyzes some c # syntaxes.

Source code
Using System; using System. collections; using System. collections. generic; using System. IO; using System. linq; using System. text; using System. threading. tasks; simplified namespace to traditional Chinese characters {class Program {static void Main (string [] args) {string [] simpleStrs = File. readAllLines (@ "E: \ code \ test \ 文.txt", Encoding. default); string simpleStr = String. join ("", simpleStrs); string [] complexStrs = File. readAllLines (@ "E: \ code \ test \ traditional Chinese character .txt", Encoding. default); string complexStr = String. join ("", complexStrs); // Console. writeLine (complexStr); Hashtable transform = new Hashtable (); for (int I = 0; I <simpleStr. length; I ++) {transform. add (simpleStr [I], complexStr [I]);} Console. writeLine ("enter a sentence"); string originalStr = Console. readLine (); StringBuilder stb = new StringBuilder (); for (int I = 0; I <originalStr. length; I ++) {if (transform. contains (originalStr [I]) {stb. append (transform [originalStr [I]);} else {stb. append (originalStr [I]) ;}} Console. writeLine ("converted to traditional Chinese:"); Console. writeLine (stb); Console. readKey ();}}}
Program running result

Value Type and reference type

Differences:
1. The value type and reference type are stored in memory differently.
2. When the value type and the reference type are passed, the transfer method is different.
Value types: int, double, bool, struct, and enum are stored in the stack.
Reference Type: string, custom class, etc. The value of the application type exists in the heap, and the address in the heap is stored in the stack.
4. immutable character string: the content in the heap is immutable. This content is unique, so if you have many strings called "abc", actually "abc" is in, stack content can be changed.
5. You can view the memory address in the real-time window.
6. what if you want to change a character in a string? First, convert the string to an array of the char type. This array exists in the stack and calls ToCharArray (); returns an array of the char type, then change and call the string (char []) constructor to generate a new String;
7. When We splice and assign values to strings, a large amount of garbage will be generated in the memory. This requires StringBuilder,

String Method
Function Name Function
Equals () Comparison string. The second parameter can be set to StringComparison. OrdinalIgnoreCase.
Split () String segmentation. The first parameter can be an array composed of unnecessary characters, and the second parameter is StringSplitOption. removeEmptyEntries removes null items and returns the split string array. It can be used to process formatted data such as the Jeson data;
IndexOf Position of the first occurrence of a sequence in a string
Trim () Removes spaces on both sides of a string.
Join () Format the string array as a string
Inheritance considerations

The inherited constructor can only be written to the list of sub-class constructor: base (,) cannot be written to: parent class name (,,,)

When the subclass hides the member method of the parent class, you can add the keyword new before the return type to hide the member inherited from the parent class. The hidden consequence is that the subclass cannot call the member of the parent class;

Set Rys conversion:

That is, subclass objects can be assigned to parent class references (implicit conversion). If the parent class references a subclass object, the parent class can be forcibly converted to a subclass (explicit conversion ), of course, you can use is or as to determine whether it is the object;
Is: determines the type. If yes, true is returned. Otherwise, false is returned. You need to test the situation between classes that are irrelevant;
As: Performs strong conversion. If the conversion succeeds, the converted object is returned. If the conversion fails, null is returned;

ArrayList

Is a set: We mainly use generic types. This type does not use a certain Array type, and the length is variable;
To add a single data, it doesn't matter if you reference it. A collection of a lot of data has two advantages over arrays. The types can be different and the length can be variable.

Function Name Function
Add (object) Any object of an object can be stored.
AddRange Used to add a set
Clear Remove all elements
Remove Delete by Object
RemoveAt Delete according to subscript
RemoveRange Delete according to subscript range
Contains Determines whether the specified object is included.
Sort Sorting. It makes no sense to sort different types of elements.
Reverse Reverse Order
Insert Insert object
InsertRange Insert a certain range of elements

There is a Count attribute that is the same as the Length attribute of other objects.
The size () Capacity attribute indicates that the allocated Capacity is 4 at the beginning and is doubled each time.

Hashtable

Is a set of key-value pairs; (table starts with lowercase)
In a key-value pair set, values are searched based on keys and subscript is performed using keys. The foreach loop is used to traverse the key-value pair set ,;
Hashtable has two important attributes: Keys is the set of Keys, and Values is the set of Values. The key can only appear once, and the value can appear many times;

Function Name Function
Add (key, value) Try to add a key-value pair in the existing HashTable
Contains (key) Determine whether a key is included in the existing HashTable
ContainsKey (key) Determine whether a key is included in the existing HashTable
ContainsValue (value) Determine whether the existing HashTable contains a value

ContainsValue
You can use Add when adding a key-value pair, or use ht [Key] = value (call insert). You can use Contains and ContainsKey to determine whether a key is included, use ContainsValue to determine whether a value is included;

Foreach

Foreach is less efficient than;

Var

C # is a strong type language, and the type of each variable must be clearly defined in the Code;
Var is used in the weak type. var can infer the type based on the value. The object has a GetType to obtain the type of this object. Implicit local variables must be initialized. var is generally used in foreach.

Path

Is a static class
In System. IO,

Function Name Function
GetFileName ("full file path ") Get the file name of the current file
GetFileNameWithoutExtension Get the file name excluding the extension
GetExtension Get the file extension
GetDirectoryName Get the name of the current folder
GetFullPath Obtain the full path of the current file

String [] contents = File. ReadAllLines (path, Encoding. Default );
Directory. GetFiles (path, "*. txt"); // The second option is regular expression matching. This is the static method of the Directory class. This returns the file name with the Directory name.
The DirectoryInfo class object has a GetFiles and EnumerateFiles method. The returned file name does not contain the directory name;
The DirectoryInfo class has other instance methods GetDirectory and EnumerateDirectory () to obtain information about subfolders;

File class

Is a static class

Method Name Function
Create ("") Try to create a file based on the directory. If the file already exists, the intercepted content is blank.
Delete ("") Tries to delete an object according to the directory. If the object does not exist
Copy (source file, target file) Copy the source file and try to paste it to the target file.
Move (source file, target file) Move one file to another
ReadAllBytes () Read a byte array from a file
Encoding. Default. GetString (byte array) Converts a byte array to a string.
Encoding. Default. GetBytes (string) Converts a string to a byte array.
WriteAllBytes () Write the byte array into the file

Related Article

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.