How do I identify a char array character element, split a string character, and show it in parallel?

Source: Internet
Author: User
Tags foreach split string back tostring
Array let's take a look at the ToCharArray () and Split () methods because it's related to the problem.

First look at the ToCharArray () method, which copies the characters from this instance to a Unicode character array, which belongs to a string class method that converts a string back to an array of characters, such as:

String Str= "This is";

Char[] arr;

Arr=str. ToCharArray ();//Separates "This is string" as a single character and becomes an element of a char array

If we want to output a look (suppose there is a ListBox control) the method is as follows:

foreach (char[] C in arr)

{

LISTBOX1.ITEMS.ADD (C.tostring ());

}

The result is this:

T

H

I

S



I

S

Looking at this result, you can see that the elements including the spaces are separated into the char array.

Let's look at the Split () method:

The Split () method identifies substrings in this instance that are delimited by one or more characters specified in the array, and then puts those substrings into a string array.

It has two overloaded versions:

Public string[] Split (params char[]);

And

Public string[] Split (char[], int);

We talk about the first version, and the latter version is explained in MSDN.

Its argument is a char[] array, which is the identity of the element in char[] as the partition, and the segmented result is stored in the string[] array, for example:

String stra= "What is this?,oh,orange!";

String[] ARSTR;

Arstr=stra.split (str. ToCharArray ());//str is the "This is" that we defined earlier

The elements in the arstr[] array are the elements that are created after the stra character is identified by 7 characters in Str

Let's take a look at the results as examples:

Foreach (String cc in ARSTR)

{

LISTBOX1.ITEMS.ADD (CC. ToString ());

}

The result is:

Wa







?, O

, orange!

String[] An array of arstr characters that have elements in a arr array do not appear because they are used as split identifiers.

At this point, you may understand the character segmentation and the relationship between the array, if it is, I will be satisfied!



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.