Use of indexof () and Split () in C #

Source: Internet
Author: User

Use of indexof in C #

Finds the first occurrence of a specified character or string in a string, and returns the index value, such as:

Str1. IndexOf ("Word");//Find the index value (position) of "word" in str1Str1. IndexOf ("Word string");//Find the index value (position) of the first character of the "string" in Str1Str1. IndexOf ("Word", start,end);//find the end character from str1 start+1 characters, find the position of the word in the string STR1 [from the first character] Note: Start+end cannot be greater than the length of a str1The indexof parameter is string, looking for the position of the first occurrence of the argument string in the string and returning the position. such as String s="0123DFDFDF";intI=s.indexof ("DF"); then i==4。


If you need more powerful string parsing, you should use a Regex class to match strings with regular expressions.

IndexOf (): Position characters and strings from the front to back in a string; all return values refer to the absolute position of the string, or 1 if null.
 string  test= " asdfjsdfjgkfasdsfsgfhgjgfjgdddd   " ;test.indexof (' d ')  =2  //  test.indexof (' d ', 1 ) =2  //   position d from the first occurrence of the third string  test.indexof (' d ', 5 , 2 ) =6  //  front-to-back position d from 5th, check 2 bits, i.e. from 5th to 7th;  
LastIndexOf (): Positions characters and strings in a string from the back forward; usage is exactly the same as indexof ().

Use of Split () in C #
The split () method splits the specified string by a specified delimiter, and the split will form an array of strings and return.
Such as:
stringstr ="aa.bb.cc.dd";string[] Strarray = str. Split ('.'), the resulting Strarray value isstring[]{"AA","BB","cc","DD"} where"AA","BB","cc","DD"That is the element that makes up the array strarray.

1. Each element in the array corresponds to an index value, as if each row of data records in the database table has its own index ID
2, the index value of the array element is counted from 0, that is, the index value of the first element is 0, followed by 1
We can use the index value of the array to take the value of the array element corresponding to the position, for example, we want to take the first element of the array "AA", then we can write:
String aa = Strarray[0];

Note:split ('. ') [1] is an abbreviated form, take it apart to see the actual is:
Start with split ('. ') method to use the string "." The cut forms a string array, and then the value of the second element in the resulting array is fetched by index [1].







Use of indexof () and Split () in C #

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.