3rd index of a character

Last post 06-24-2009 8:46 AM by anand.supner. 4 replies.

Sort Posts:

  • 3rd index of a character

    06-24-2009, 7:33 AM
    • Member
      4 point Member
    • Divya1012
    • Member since 06-21-2009, 9:19 AM
    • Posts 8

     I have to get  the 3rd index of '.'(fullstop).I have retrieved the first index but want the third.

    Please help.

  • Re: 3rd index of a character

    06-24-2009, 8:14 AM
    Answer

    Something like this:

    int index = 0;
    string text = "this.is a test.string.to return.3rd one.";
    for (int loop = 0; loop
    index = text.IndexOf('.', index + 1);

  • Re: 3rd index of a character

    06-24-2009, 8:15 AM
    • Contributor
      2,425 point Contributor
    • diseirra
    • Member since 01-16-2008, 9:08 AM
    • Posts 526

     try this

    Dim str As String = "ht.tt.po.fd"

    Dim cnt = 0

    Dim ina = str.IndexOf(".")

    cnt = ina

    str = str.Substring(ina + 1, str.Length - 1 - ina)

    cnt = cnt + ina + 1

    ina = str.IndexOf(".")

    str = str.Substring(ina + 1, str.Length - 1 - ina)

    ina = str.IndexOf(".")

    cnt = cnt + ina + 1

    now this cnt has got the index of the 3rd dot.

     

    Diseirra
    Mark it as an answer, if it helped you anyhow...
  • Re: 3rd index of a character

    06-24-2009, 8:41 AM
    • Star
      12,555 point Star
    • malcolms
    • Member since 06-12-2008, 12:38 AM
    • Melbourne, Australia
    • Posts 2,061

    You can use the Split method for this:

    string words = "Some.words.here.for.testing";
    string word = words.Split('.').ElementAt(2);

    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: 3rd index of a character

    06-24-2009, 8:46 AM
    • Member
      46 point Member
    • anand.supner
    • Member since 06-21-2009, 1:52 PM
    • Posts 15

    Here is another way 


       // using System.Text.RegularExpressions

                string inputString = "The cat saw the other cats playing in the back yard.";
                string pattern = "cat";
                MatchCollection matches = Regex.Matches(inputString, pattern);
                foreach (Match match in matches)
                    Response.Write("Value: " + match.Value + ", Index= " + match.Index + "<BR>"); 

                string inputString = "The cat saw the other cats playing in the back yard.";

                string pattern = "cat";


                MatchCollection matches = Regex.Matches(inputString, pattern);

                foreach (Match match in matches)

                    Response.Write("Value: " + match.Value + ", Index= " + match.Index + "<BR>"); 


Page 1 of 1 (5 items)