The following is the use of C # string and Hashtable class to implement a simplified character to the function of traditional Chinese characters, and then analyze some C # syntax
Source
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceSimplified Chinese to traditional Chinese characters {classprogram {Static voidMain (string[] args) {string[] Simplestrs = File.ReadAllLines (@"E:\code\test\ simplified.", Encoding.default);stringSimplestr = String.Join ("", simplestrs);string[] Complexstrs = File.ReadAllLines (@"E:\code\test\ Traditional Chinese characters" txt ", Encoding.default);stringComplexstr = String.Join ("", complexstrs);//console.writeline (COMPLEXSTR);Hashtable transform=NewHashtable (); for(inti =0; i < simplestr.length; i++) {transform. ADD (Simplestr[i], complexstr[i]); } Console.WriteLine ("Please enter a sentence");stringOriginalstr = Console.ReadLine (); StringBuilder STB =NewStringBuilder (); for(inti =0; i < originalstr.length; i++) {if(Transform. Contains (Originalstr[i])) {STB. Append (Transform[originalstr[i]); }Else{STB. Append (Originalstr[i]); }} Console.WriteLine ("translate to Traditional Chinese as:"); Console.WriteLine (STB); Console.readkey (); } }}
Program Run Results
Value types and reference types
Difference:
1. Value types and reference types are not stored in the same place in memory
2. When passing value types and reference types, they are not passed in the same way.
Value types: int, double, bool, struct, enum, etc. are stored in the stack.
Reference type: string, custom class, and so on, the value of the application type exists in the heap, and the address in the heap is stored in the stack.
4. String immutability: Refers to the contents of the heap is not mutable, this content is unique, so if you have a lot of string is called "abc", in fact, "ABC" in, the contents of the stack can be changed.
5. The Immediate window can view the memory address.
6. What to do if you want to change a character in a string, first convert the string to an array of type char, which has a stack area, calls ToCharArray (), returns an array of type char, then changes, and then calls string (char []) This constructor then generates a new string;
7. We do a lot of concatenation of the string assignment operation, the memory will produce a lot of garbage, this is we need StringBuilder,
Method of String
Name of function |
function |
Equals () |
Compare strings, the second parameter can be set to StringComparison.OrdinalIgnoreCase |
Split () |
String segmentation, the first parameter can be an array of those characters that are not needed, the second parameter stringsplitoption.removeemptyentries to remove the empty items, return a segmented array of strings, can be used to process Jeson data and other formatted data; |
IndexOf |
The position of the first occurrence of a sequence in a string |
Trim () |
Remove spaces on both sides of a string |
Join () |
To format a string array as a string |
Inheritance considerations
The inheritance constructor can only be written in the function list of the subclass constructor: base (,,) cannot be written: Parent class name (,,,)
When a subclass hides a member method of a parent class, it is possible to add the keyword new before the return type to hide the member that inherits from the parent class, and the hidden consequence is that the subclass cannot call the members of the parent class;
Set Richter conversion:
Is that the subclass object can be assigned to the parent class reference (implicit conversion), if the parent class refers to a subclass object, you can forcibly convert the parent class to a subclass (explicit conversion), of course, is or as can be used to determine whether the object;
is: The type of judgment, if yes that returns true otherwise returns false; you need to test the situation between classes that do not have relationships;
As: Perform a strong turn, and if successful returns the converted object, the failure returns null;
ArrayList
is a collection: we mainly use the generic type, this type does not use the Array type is certain, and then the length is variable;
To add a single data, in fact, are references do not matter. A collection of many data, with two major benefits relative to an array: types can be different and have variable lengths.
function name |
action |
Add (obje CT) |
|
addRange |
When you add a collection, use the /td> |
Clear |
remove all elements |
remove |
|
RemoveAt |
|
Remo Verange |
|
Contains |
|
sort |
sort of, some different types of elements do not make sense |
Reverse |
reverse |
Insert |
Insert object |
insertrange |
insert a range of elements |
There is a Count property that is the same as the length property of another object.
The size () capacity property indicates that the allocated capacity has just started at 4, and then expands by one time.
Hashtable
is a set of key-value pairs; (table starts with lowercase)
In the set of key-value pairs, it is based on the key to find the value, with the key to do subscript; Use the Foreach loop to iterate through the set of key-value pairs;
There are two important attributes in Hashtable. One is that keys is a collection of keys, and one is a collection of values. The key can only appear once, the value may appear many times;
Name of function |
function |
ADD (Key,value) |
Attempting to add a key-value pair inside an existing hashtable |
Contains (Key) |
Determine if a key is included in an existing Hashtable |
ContainsKey (Key) |
Determine if a key is included in an existing Hashtable |
Containsvalue (value) |
Determine if a value is included in an existing Hashtable |
Containsvalue
You can use add when adding a key-value pair, or you can use the ht[key]= value (call insert); You can use contains and containskey to determine if a key is included, and containsvalue to determine if a value is included;
Foreach
The efficiency of foreach is a little lower than for.
Var
C # is a strongly typed language that must have a clear definition of the type of each variable in the code;
var is used in weakly typed, Var can be inferred by value, and object has a gettype to get the type of the object. Implicitly-typed local variables must be initialized, and VAR is typically used in foreach
Path
is a static class
Inside the System.IO,
Name of function |
function |
GetFileName ("Full path to File") |
Gets the filename of the current file |
Getfilenamewithoutextension |
Get file names that do not include extensions |
GetExtension |
Get the file extension |
GetDirectoryName |
Get the name of the current folder |
GetFullPath |
Gets the full path of the current file |
string [] Contents=file.readalllines (Path,encoding.default);
Directory.GetFiles (Path, "*.txt");//The second option is a regular expression match; This is a static method of the Directory class; This returns the file name with the directory name
The object of the DirectoryInfo class has a method of GetFiles and Enumeratefiles, and returns a file name without a directory name;
The DirectoryInfo class has additional instance methods Getdirectory and Enumeratedirectory () can get information about subfolders;
File class
is a static class
Method Name |
function |
Create ("") |
Attempts to create a file according to the directory, if the file already exists the interception content is empty |
Delete ("") |
Attempts to delete a file according to the directory if the file does not exist |
Copy (source file, destination file) |
Copy the source file and try to paste it into the destination file |
Move (source file, destination file) |
Move a file to another place |
ReadAllBytes () |
Reading a byte array from a file |
Encoding.Default.GetString (byte array) |
Convert a byte array to a string |
Encoding.Default.GetBytes (String) |
Convert a string to a byte array |
WriteAllBytes () |
Write a byte array into the file |
C # application--Simplified to Traditional Chinese