(C #) retrieves the position (IndexOf) of the nth occurrence of a character in a string)

Source: Internet
Author: User

[Csharp] class Program {static void Main (string [] args) {Console. writeLine ("Please input your STRING:"); string a = Console. readLine (); char c; char find_c; Console. writeLine ("Please input the CHARACTER in your string:"); find_c = Convert. toChar (Console. readLine (); int count = 0; www.2cto.com for (int I = 0; I <. length; I ++) {c = a [I]; if (c = find_c) // obtain the number of characters in, to traverse the {count ++;} Console. writeLine ("There are total {0} of char '{1}' in your input string. ", count, find_c); int index = 0; int n; // the nth find_c Console. writeLine ("Please input the SEQUENCE of the char '{0}' in your input string:", find_c); n = Convert. toInt32 (Console. readLine (); if (n> count) {Console. writeLine ("Error: The Num must be less than or equal to {0 }. ", count); Console. readKey (); return ;}for (int j = 1; j <= count; j ++) {index =. indexOf (find_c, index); if (j = n) {break;} else {index =. indexOf (find_c, index + 1) ;}} Console. writeLine ("The Index of the No. {0} char '{1}' in your input string is {2 }. ", n, find_c, index); Console. readKey ();}}

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.