How to quickly process strings in C #

Source: Internet
Author: User
Tags truncated

String segmentation

We often encounter the problem of string segmentation. When making a player later, we can use the method we learned to separate the lyrics.

Next let's take a look at the string [] Split (Params char [] separator): splits the string into a string array according to the specified delimiter:

 

Figure 1. string segmentation

String [] Split (char [] separator, stringsplitoptions options) splits the string into a String Array Based on the specified char separator (when options is removed from removeemptyentries, the blank string in the result is removed );

 

Figure 2. See what is different from the previous one

 

Figure 3. Remove blank items

If a user inputs a Chinese character, how can we extract it?

When learning, we will focus on the methods in string, because the methods in string are comprehensive. Here we will introduce the basic knowledge in detail for beginners to refer, the purpose is to avoid detours in the future.

Next we will look at other common methods in string functions.

String replacement: String Replace (string oldvalue, string newvalue) replaces the oldvalue in the string with newvalue. Example: replace the name.

 

Figure 4 Replacement of Characters

 

Figure 5 replacement of strings

Substring: string substring (INT startindex), starting from startindex to the final substring;

 

Figure 6. truncate a string

Substring (); the first overload parameter is of the int type. Given a number, it is truncated from this subscript. Capture to the end

Substring (); the second overload. The two parameters are of the int type. The first parameter is truncated starting from which subscript, and the second parameter captures several strings;

 

If the number of interceptions exceeds the length of the string, an exception is reported.

 

Figure 7. Determine whether the string contains this string

Bool startswith (string value) determines whether the string starts with a substring value;

 

Figure 8. Determine if the string starts with Sichuan

Bool endswith (string value) determines whether the string ends with a substring value;

 

Figure 9. Judging from the back

It is opposite to the method above, and determines whether it ends with a string.

Int indexof (string value): the first occurrence position of the substring value.

 

Figure 10. Search for e in a string

One interview question

What is the difference between stringbuilder (SB) and string?

A: A new instance is generated when string operations (such as value assignment and concatenation) are performed, while stringbuilder does not, therefore, it is best to use stringbuilder when splicing a large number of strings or frequently performing operations on a string. Do not use string

If you want to operate on a growing string, try not to use the stringbuilder class for the string class. The two classes work in different ways. The string class is a traditional method for modifying strings, it can indeed complete the process of adding a string to another string, but in the net framework, this operation is not possible, because the system first sets the object, it takes a lot of time to read the data in the memory and pay it to the object. The stringbuilder class below the system text namespace is not like this. It provides the append method, which can be used to modify strings in the original place of existing objects, simple and direct. Of course, the efficiency difference between the two is usually not noticed, but if you want to add a large number of operations to a string, the time consumed by the stringbuilder class and the string class are not an order of magnitude.

 

Exercise

1. Receive user input strings and output the characters in the opposite order of input. "ABC" → "CBA" --------- learned before

 

2. receive an English sentence from the user and output the words in reverse order. "Hello C sharp" → "sharp C hello" ----------- same as above

If I was your leader, now let you do the two exercises above to see if you can write the above two questions in a simple way.Code?

 

I focus on the following method, question: extracted from the e-mail user name and domain name: abc@163.com. ---------- Split ------------

 

Figure 11. Frequently used methods after indexof

Let the user input a sentence to find all e locations

 

Figure 12. Find all e in the string

Exercise: multiple files are stored in text files.ArticleTitles, authors, titles, and authors are separated by several spaces (a certain number of spaces). Each line has a long or short title. A maximum of 10 titles can be output to the console. If the title length exceeds 10, then, the Child string with the length of 8 is truncated and "..." is added at the end. add a vertical line to output the author's name.

 

 

Figure 13. Read the article content for judgment

 

Figure 14. final demonstration of judgment by reading articles

Insert in this example, insert a specified string instance at the specified index location

 

Figure 15 insert

At this stage of learning, we will surely see that there are still a lot of methods we have learned. As long as we don't give up, we will have the spirit of making three or more hammers. The road to the future will be bright. Note: You need to find some exercises and repeat the code several times.

 

Figure 16. judge whether the string is null

 

Figure 17. add content to a string

 

Figure 18. Remove

 

Figure 19. Trim

Today, more operations are performed on the console, which is a bit boring. Some people say that people who learn C # Will only drag controls. After reading the above article, is the control really only so simple? Much of the content written here is followed by winform, which is also a form application.Program. For C # fans, make sufficient preparations when writing projects later. I also hope that my efforts will help beginners better interpret C.

List of recent articles by the author:
C # basic tutorial (free of charge, the best gift for code lovers. Note: The authors share their carefully organized basic C # tutorials without any commercial purposes. I hope to share my experiences with more code lovers. Please give me more advice !!!)
Process-oriented Comparison of three cycles
Method (I) in C)
Our common Array
Object-oriented Thought Change
C # super-class and easy-to-use classes
The use of destructor and namespaces in C #
C # super-class and easy-to-use strings
GDI + The GDI + code I have collected over the years
The GDI + Code 2 I collected over the years
HTML Overview HTML language that you cannot ignore
You can't ignore HTML 2.
HTML language 3 that you cannot ignore

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.