Programming pleasure: C # truncate long strings by string

Source: Internet
Author: User

I believe everyone has used it, but I think it is quite helpful, because it is always used to intercept code at ordinary times, so I 'd like to share it with you.
Generally, you can use the built-in split of string, but the split can only be intercepted by char type, which is not very convenient.
Truncate by string and use a regular expression. A simple two-line code solves many problems.


As follows:

String content = @ "Long character"; Regex regex = new Regex (@ "truncated string"); string [] result = regex. Split (content );

Example:
Class Program {static void Main (string [] args) {string content = @ "carrying a paper umbrella,
Wandering alone in the long, long, and lonely Rain Lane,
I hope that
The same as a clove
Sad girl.
She has
The same color as clove,
The fragrance of clove,
Clove-like sorrow,
Grief in the rain,
Sorrow and sorrow;
She is wandering in the lonely Rain Lane,
Oil Paper Umbrella
Like me,
Like me
With
Indifference, clear, and melancholy.
She approached silently,
Approaching and throwing
An ordinary eye
She floated
Like a dream,
It is as miserable and confused as a dream.
Float like a dream
A clove,
This girl floated beside me;
She was far away silently, far away,
To the p branch wall,
Go through the Rain Lane.
In the song of rain,
Remove her color,
The fragrance of her,
Dissipated,
Even her
Eye-catching,
Clove-like melancholy.
Holding a paper umbrella, alone
Long and long
The lonely Rain Lane,
I want to float
The same as a clove
Sad girl. "; Regex regex = new Regex (@"
"); String [] result = regex. Split (content); foreach (string item in result) {Console. WriteLine (item) ;}console. ReadLine ();}}
Result chart:


Although skillful, it can improve work efficiency.

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.