C# IndexOf, when word is part of another word, How to?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















let's say I have string "soak oak" and I want to have string index of ("oak"), it returns me the index of where "oak" starts in "soak" (1) but I want to find index of exact word "oak" (5), what do I need to do?



string text = "soak oak";
char seperators = {' ', '.', ',', '!', '?', ':',
';', '(', ')', 't', 'r', 'n', '"', '„', '“'};
string parts = text.Split(seperators,
StringSplitOptions.RemoveEmptyEntries);
text.IndexOf("oak"); // gets '1' because "oak" is in "soak"
// but I want to get 5 because of exact word "oak"









share|improve this question




















  • 2





    You are searching inside the string text instead of the string parts.

    – Peter B
    Nov 22 '18 at 10:59






  • 2





    you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"

    – James
    Nov 22 '18 at 10:59











  • You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!

    – Pranesh Janarthanan
    Nov 22 '18 at 11:08











  • You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb

    – DParry
    Nov 22 '18 at 11:15


















1















let's say I have string "soak oak" and I want to have string index of ("oak"), it returns me the index of where "oak" starts in "soak" (1) but I want to find index of exact word "oak" (5), what do I need to do?



string text = "soak oak";
char seperators = {' ', '.', ',', '!', '?', ':',
';', '(', ')', 't', 'r', 'n', '"', '„', '“'};
string parts = text.Split(seperators,
StringSplitOptions.RemoveEmptyEntries);
text.IndexOf("oak"); // gets '1' because "oak" is in "soak"
// but I want to get 5 because of exact word "oak"









share|improve this question




















  • 2





    You are searching inside the string text instead of the string parts.

    – Peter B
    Nov 22 '18 at 10:59






  • 2





    you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"

    – James
    Nov 22 '18 at 10:59











  • You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!

    – Pranesh Janarthanan
    Nov 22 '18 at 11:08











  • You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb

    – DParry
    Nov 22 '18 at 11:15














1












1








1








let's say I have string "soak oak" and I want to have string index of ("oak"), it returns me the index of where "oak" starts in "soak" (1) but I want to find index of exact word "oak" (5), what do I need to do?



string text = "soak oak";
char seperators = {' ', '.', ',', '!', '?', ':',
';', '(', ')', 't', 'r', 'n', '"', '„', '“'};
string parts = text.Split(seperators,
StringSplitOptions.RemoveEmptyEntries);
text.IndexOf("oak"); // gets '1' because "oak" is in "soak"
// but I want to get 5 because of exact word "oak"









share|improve this question
















let's say I have string "soak oak" and I want to have string index of ("oak"), it returns me the index of where "oak" starts in "soak" (1) but I want to find index of exact word "oak" (5), what do I need to do?



string text = "soak oak";
char seperators = {' ', '.', ',', '!', '?', ':',
';', '(', ')', 't', 'r', 'n', '"', '„', '“'};
string parts = text.Split(seperators,
StringSplitOptions.RemoveEmptyEntries);
text.IndexOf("oak"); // gets '1' because "oak" is in "soak"
// but I want to get 5 because of exact word "oak"






c# string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 11:40









Dmitry Bychenko

112k1099142




112k1099142










asked Nov 22 '18 at 10:56









GoodnightJessGoodnightJess

255




255








  • 2





    You are searching inside the string text instead of the string parts.

    – Peter B
    Nov 22 '18 at 10:59






  • 2





    you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"

    – James
    Nov 22 '18 at 10:59











  • You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!

    – Pranesh Janarthanan
    Nov 22 '18 at 11:08











  • You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb

    – DParry
    Nov 22 '18 at 11:15














  • 2





    You are searching inside the string text instead of the string parts.

    – Peter B
    Nov 22 '18 at 10:59






  • 2





    you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"

    – James
    Nov 22 '18 at 10:59











  • You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!

    – Pranesh Janarthanan
    Nov 22 '18 at 11:08











  • You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb

    – DParry
    Nov 22 '18 at 11:15








2




2





You are searching inside the string text instead of the string parts.

– Peter B
Nov 22 '18 at 10:59





You are searching inside the string text instead of the string parts.

– Peter B
Nov 22 '18 at 10:59




2




2





you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"

– James
Nov 22 '18 at 10:59





you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"

– James
Nov 22 '18 at 10:59













You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!

– Pranesh Janarthanan
Nov 22 '18 at 11:08





You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!

– Pranesh Janarthanan
Nov 22 '18 at 11:08













You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb

– DParry
Nov 22 '18 at 11:15





You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb

– DParry
Nov 22 '18 at 11:15












6 Answers
6






active

oldest

votes


















0














We can test indexes (IndexOf) in a loop:



static HashSet<char> s_Separtors = new HashSet<char>() {
' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
};

private static int WordIndexOf(string source, string toFind) {
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
return -1;

for (int index = source.IndexOf(toFind);
index >= 0;
index = source.IndexOf(toFind, index + 1)) {
if (index < 0)
return -1;

if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
(index >= source.Length - toFind.Length ||
s_Separtors.Contains(source[index + toFind.Length])))
return index;
}

return -1;
}


Demo:



// 5
Console.Write(WordIndexOf("soak oak", "oak"));





share|improve this answer































    3














    Regex approach



    string text = "soak oak";
    int result = Regex.Match(text, @"boakb").Index;





    share|improve this answer





















    • 1





      or you could use "boakb", which looks for word boundaries

      – Robin Bennett
      Nov 22 '18 at 11:08











    • string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }

      – Aakarsh Dhawan
      Nov 22 '18 at 11:14











    • string text = "I can see antoak." Expected: 13, actual: 0

      – Dmitry Bychenko
      Nov 22 '18 at 11:16











    • @RobinBennett thanks, I've updated my code to your suggestion

      – fubo
      Nov 22 '18 at 12:18



















    1














    You may use below regex to find exact word in your string.



    string text = "soak oak";
    string searchText = "oak";
    var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;


    Output:



    5


    See the demo






    share|improve this answer


























    • @"b" + Regex.Escape(searchText) + @"b" to be on the safe side

      – Dmitry Bychenko
      Nov 22 '18 at 11:18











    • @DmitryBychenko, thanks for improvement, answer updated :)

      – er-sho
      Nov 22 '18 at 11:19



















    0














    You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:



    string text = "soak oak";
    var pattern = @"boakb";
    var regex = new Regex(pattern);
    foreach(Match m in regex.Matches(text)){
    Console.WriteLine(m.Index);
    Console.WriteLine(m.Value);
    }





    share|improve this answer































      0














      You could find the string in your array by converting it to a list and using the IndexOf() method.



      parts.ToList().IndexOf("oak");


      That tells you which array item it is, rather than the index in the original string.






      share|improve this answer































        0














        Another RegEx approach-



            string text = "soak oak";
        var match = Regex.Match(text, @"s[oak]");
        if (match.Success)
        {
        Console.WriteLine(match.Index); // 4
        }



        • s White space


        Hope it helps.






        share|improve this answer
























        • [oak] is inccorect here: counter example is "a kao is not an oak"

          – Dmitry Bychenko
          Nov 22 '18 at 11:19













        • @DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?

          – Aakarsh Dhawan
          Nov 22 '18 at 11:40













        • "oak" is a counter example for @"s(oak)": we don't want explicit s in the pattern; please, have a look at er-shoaib's answer

          – Dmitry Bychenko
          Nov 22 '18 at 11:42














        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429394%2fc-sharp-indexof-when-word-is-part-of-another-word-how-to%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        We can test indexes (IndexOf) in a loop:



        static HashSet<char> s_Separtors = new HashSet<char>() {
        ' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
        };

        private static int WordIndexOf(string source, string toFind) {
        if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
        return -1;

        for (int index = source.IndexOf(toFind);
        index >= 0;
        index = source.IndexOf(toFind, index + 1)) {
        if (index < 0)
        return -1;

        if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
        (index >= source.Length - toFind.Length ||
        s_Separtors.Contains(source[index + toFind.Length])))
        return index;
        }

        return -1;
        }


        Demo:



        // 5
        Console.Write(WordIndexOf("soak oak", "oak"));





        share|improve this answer




























          0














          We can test indexes (IndexOf) in a loop:



          static HashSet<char> s_Separtors = new HashSet<char>() {
          ' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
          };

          private static int WordIndexOf(string source, string toFind) {
          if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
          return -1;

          for (int index = source.IndexOf(toFind);
          index >= 0;
          index = source.IndexOf(toFind, index + 1)) {
          if (index < 0)
          return -1;

          if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
          (index >= source.Length - toFind.Length ||
          s_Separtors.Contains(source[index + toFind.Length])))
          return index;
          }

          return -1;
          }


          Demo:



          // 5
          Console.Write(WordIndexOf("soak oak", "oak"));





          share|improve this answer


























            0












            0








            0







            We can test indexes (IndexOf) in a loop:



            static HashSet<char> s_Separtors = new HashSet<char>() {
            ' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
            };

            private static int WordIndexOf(string source, string toFind) {
            if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
            return -1;

            for (int index = source.IndexOf(toFind);
            index >= 0;
            index = source.IndexOf(toFind, index + 1)) {
            if (index < 0)
            return -1;

            if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
            (index >= source.Length - toFind.Length ||
            s_Separtors.Contains(source[index + toFind.Length])))
            return index;
            }

            return -1;
            }


            Demo:



            // 5
            Console.Write(WordIndexOf("soak oak", "oak"));





            share|improve this answer













            We can test indexes (IndexOf) in a loop:



            static HashSet<char> s_Separtors = new HashSet<char>() {
            ' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
            };

            private static int WordIndexOf(string source, string toFind) {
            if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
            return -1;

            for (int index = source.IndexOf(toFind);
            index >= 0;
            index = source.IndexOf(toFind, index + 1)) {
            if (index < 0)
            return -1;

            if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
            (index >= source.Length - toFind.Length ||
            s_Separtors.Contains(source[index + toFind.Length])))
            return index;
            }

            return -1;
            }


            Demo:



            // 5
            Console.Write(WordIndexOf("soak oak", "oak"));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 11:11









            Dmitry BychenkoDmitry Bychenko

            112k1099142




            112k1099142

























                3














                Regex approach



                string text = "soak oak";
                int result = Regex.Match(text, @"boakb").Index;





                share|improve this answer





















                • 1





                  or you could use "boakb", which looks for word boundaries

                  – Robin Bennett
                  Nov 22 '18 at 11:08











                • string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }

                  – Aakarsh Dhawan
                  Nov 22 '18 at 11:14











                • string text = "I can see antoak." Expected: 13, actual: 0

                  – Dmitry Bychenko
                  Nov 22 '18 at 11:16











                • @RobinBennett thanks, I've updated my code to your suggestion

                  – fubo
                  Nov 22 '18 at 12:18
















                3














                Regex approach



                string text = "soak oak";
                int result = Regex.Match(text, @"boakb").Index;





                share|improve this answer





















                • 1





                  or you could use "boakb", which looks for word boundaries

                  – Robin Bennett
                  Nov 22 '18 at 11:08











                • string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }

                  – Aakarsh Dhawan
                  Nov 22 '18 at 11:14











                • string text = "I can see antoak." Expected: 13, actual: 0

                  – Dmitry Bychenko
                  Nov 22 '18 at 11:16











                • @RobinBennett thanks, I've updated my code to your suggestion

                  – fubo
                  Nov 22 '18 at 12:18














                3












                3








                3







                Regex approach



                string text = "soak oak";
                int result = Regex.Match(text, @"boakb").Index;





                share|improve this answer















                Regex approach



                string text = "soak oak";
                int result = Regex.Match(text, @"boakb").Index;






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 12:17

























                answered Nov 22 '18 at 11:06









                fubofubo

                31.1k970107




                31.1k970107








                • 1





                  or you could use "boakb", which looks for word boundaries

                  – Robin Bennett
                  Nov 22 '18 at 11:08











                • string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }

                  – Aakarsh Dhawan
                  Nov 22 '18 at 11:14











                • string text = "I can see antoak." Expected: 13, actual: 0

                  – Dmitry Bychenko
                  Nov 22 '18 at 11:16











                • @RobinBennett thanks, I've updated my code to your suggestion

                  – fubo
                  Nov 22 '18 at 12:18














                • 1





                  or you could use "boakb", which looks for word boundaries

                  – Robin Bennett
                  Nov 22 '18 at 11:08











                • string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }

                  – Aakarsh Dhawan
                  Nov 22 '18 at 11:14











                • string text = "I can see antoak." Expected: 13, actual: 0

                  – Dmitry Bychenko
                  Nov 22 '18 at 11:16











                • @RobinBennett thanks, I've updated my code to your suggestion

                  – fubo
                  Nov 22 '18 at 12:18








                1




                1





                or you could use "boakb", which looks for word boundaries

                – Robin Bennett
                Nov 22 '18 at 11:08





                or you could use "boakb", which looks for word boundaries

                – Robin Bennett
                Nov 22 '18 at 11:08













                string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }

                – Aakarsh Dhawan
                Nov 22 '18 at 11:14





                string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }

                – Aakarsh Dhawan
                Nov 22 '18 at 11:14













                string text = "I can see antoak." Expected: 13, actual: 0

                – Dmitry Bychenko
                Nov 22 '18 at 11:16





                string text = "I can see antoak." Expected: 13, actual: 0

                – Dmitry Bychenko
                Nov 22 '18 at 11:16













                @RobinBennett thanks, I've updated my code to your suggestion

                – fubo
                Nov 22 '18 at 12:18





                @RobinBennett thanks, I've updated my code to your suggestion

                – fubo
                Nov 22 '18 at 12:18











                1














                You may use below regex to find exact word in your string.



                string text = "soak oak";
                string searchText = "oak";
                var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;


                Output:



                5


                See the demo






                share|improve this answer


























                • @"b" + Regex.Escape(searchText) + @"b" to be on the safe side

                  – Dmitry Bychenko
                  Nov 22 '18 at 11:18











                • @DmitryBychenko, thanks for improvement, answer updated :)

                  – er-sho
                  Nov 22 '18 at 11:19
















                1














                You may use below regex to find exact word in your string.



                string text = "soak oak";
                string searchText = "oak";
                var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;


                Output:



                5


                See the demo






                share|improve this answer


























                • @"b" + Regex.Escape(searchText) + @"b" to be on the safe side

                  – Dmitry Bychenko
                  Nov 22 '18 at 11:18











                • @DmitryBychenko, thanks for improvement, answer updated :)

                  – er-sho
                  Nov 22 '18 at 11:19














                1












                1








                1







                You may use below regex to find exact word in your string.



                string text = "soak oak";
                string searchText = "oak";
                var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;


                Output:



                5


                See the demo






                share|improve this answer















                You may use below regex to find exact word in your string.



                string text = "soak oak";
                string searchText = "oak";
                var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;


                Output:



                5


                See the demo







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 11:19

























                answered Nov 22 '18 at 11:12









                er-shoer-sho

                7,1402619




                7,1402619













                • @"b" + Regex.Escape(searchText) + @"b" to be on the safe side

                  – Dmitry Bychenko
                  Nov 22 '18 at 11:18











                • @DmitryBychenko, thanks for improvement, answer updated :)

                  – er-sho
                  Nov 22 '18 at 11:19



















                • @"b" + Regex.Escape(searchText) + @"b" to be on the safe side

                  – Dmitry Bychenko
                  Nov 22 '18 at 11:18











                • @DmitryBychenko, thanks for improvement, answer updated :)

                  – er-sho
                  Nov 22 '18 at 11:19

















                @"b" + Regex.Escape(searchText) + @"b" to be on the safe side

                – Dmitry Bychenko
                Nov 22 '18 at 11:18





                @"b" + Regex.Escape(searchText) + @"b" to be on the safe side

                – Dmitry Bychenko
                Nov 22 '18 at 11:18













                @DmitryBychenko, thanks for improvement, answer updated :)

                – er-sho
                Nov 22 '18 at 11:19





                @DmitryBychenko, thanks for improvement, answer updated :)

                – er-sho
                Nov 22 '18 at 11:19











                0














                You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:



                string text = "soak oak";
                var pattern = @"boakb";
                var regex = new Regex(pattern);
                foreach(Match m in regex.Matches(text)){
                Console.WriteLine(m.Index);
                Console.WriteLine(m.Value);
                }





                share|improve this answer




























                  0














                  You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:



                  string text = "soak oak";
                  var pattern = @"boakb";
                  var regex = new Regex(pattern);
                  foreach(Match m in regex.Matches(text)){
                  Console.WriteLine(m.Index);
                  Console.WriteLine(m.Value);
                  }





                  share|improve this answer


























                    0












                    0








                    0







                    You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:



                    string text = "soak oak";
                    var pattern = @"boakb";
                    var regex = new Regex(pattern);
                    foreach(Match m in regex.Matches(text)){
                    Console.WriteLine(m.Index);
                    Console.WriteLine(m.Value);
                    }





                    share|improve this answer













                    You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:



                    string text = "soak oak";
                    var pattern = @"boakb";
                    var regex = new Regex(pattern);
                    foreach(Match m in regex.Matches(text)){
                    Console.WriteLine(m.Index);
                    Console.WriteLine(m.Value);
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 11:11









                    Muaz OthmanMuaz Othman

                    362313




                    362313























                        0














                        You could find the string in your array by converting it to a list and using the IndexOf() method.



                        parts.ToList().IndexOf("oak");


                        That tells you which array item it is, rather than the index in the original string.






                        share|improve this answer




























                          0














                          You could find the string in your array by converting it to a list and using the IndexOf() method.



                          parts.ToList().IndexOf("oak");


                          That tells you which array item it is, rather than the index in the original string.






                          share|improve this answer


























                            0












                            0








                            0







                            You could find the string in your array by converting it to a list and using the IndexOf() method.



                            parts.ToList().IndexOf("oak");


                            That tells you which array item it is, rather than the index in the original string.






                            share|improve this answer













                            You could find the string in your array by converting it to a list and using the IndexOf() method.



                            parts.ToList().IndexOf("oak");


                            That tells you which array item it is, rather than the index in the original string.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 '18 at 11:12









                            Robin BennettRobin Bennett

                            1,925413




                            1,925413























                                0














                                Another RegEx approach-



                                    string text = "soak oak";
                                var match = Regex.Match(text, @"s[oak]");
                                if (match.Success)
                                {
                                Console.WriteLine(match.Index); // 4
                                }



                                • s White space


                                Hope it helps.






                                share|improve this answer
























                                • [oak] is inccorect here: counter example is "a kao is not an oak"

                                  – Dmitry Bychenko
                                  Nov 22 '18 at 11:19













                                • @DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?

                                  – Aakarsh Dhawan
                                  Nov 22 '18 at 11:40













                                • "oak" is a counter example for @"s(oak)": we don't want explicit s in the pattern; please, have a look at er-shoaib's answer

                                  – Dmitry Bychenko
                                  Nov 22 '18 at 11:42


















                                0














                                Another RegEx approach-



                                    string text = "soak oak";
                                var match = Regex.Match(text, @"s[oak]");
                                if (match.Success)
                                {
                                Console.WriteLine(match.Index); // 4
                                }



                                • s White space


                                Hope it helps.






                                share|improve this answer
























                                • [oak] is inccorect here: counter example is "a kao is not an oak"

                                  – Dmitry Bychenko
                                  Nov 22 '18 at 11:19













                                • @DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?

                                  – Aakarsh Dhawan
                                  Nov 22 '18 at 11:40













                                • "oak" is a counter example for @"s(oak)": we don't want explicit s in the pattern; please, have a look at er-shoaib's answer

                                  – Dmitry Bychenko
                                  Nov 22 '18 at 11:42
















                                0












                                0








                                0







                                Another RegEx approach-



                                    string text = "soak oak";
                                var match = Regex.Match(text, @"s[oak]");
                                if (match.Success)
                                {
                                Console.WriteLine(match.Index); // 4
                                }



                                • s White space


                                Hope it helps.






                                share|improve this answer













                                Another RegEx approach-



                                    string text = "soak oak";
                                var match = Regex.Match(text, @"s[oak]");
                                if (match.Success)
                                {
                                Console.WriteLine(match.Index); // 4
                                }



                                • s White space


                                Hope it helps.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 22 '18 at 11:13









                                Aakarsh DhawanAakarsh Dhawan

                                1196




                                1196













                                • [oak] is inccorect here: counter example is "a kao is not an oak"

                                  – Dmitry Bychenko
                                  Nov 22 '18 at 11:19













                                • @DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?

                                  – Aakarsh Dhawan
                                  Nov 22 '18 at 11:40













                                • "oak" is a counter example for @"s(oak)": we don't want explicit s in the pattern; please, have a look at er-shoaib's answer

                                  – Dmitry Bychenko
                                  Nov 22 '18 at 11:42





















                                • [oak] is inccorect here: counter example is "a kao is not an oak"

                                  – Dmitry Bychenko
                                  Nov 22 '18 at 11:19













                                • @DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?

                                  – Aakarsh Dhawan
                                  Nov 22 '18 at 11:40













                                • "oak" is a counter example for @"s(oak)": we don't want explicit s in the pattern; please, have a look at er-shoaib's answer

                                  – Dmitry Bychenko
                                  Nov 22 '18 at 11:42



















                                [oak] is inccorect here: counter example is "a kao is not an oak"

                                – Dmitry Bychenko
                                Nov 22 '18 at 11:19







                                [oak] is inccorect here: counter example is "a kao is not an oak"

                                – Dmitry Bychenko
                                Nov 22 '18 at 11:19















                                @DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?

                                – Aakarsh Dhawan
                                Nov 22 '18 at 11:40







                                @DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?

                                – Aakarsh Dhawan
                                Nov 22 '18 at 11:40















                                "oak" is a counter example for @"s(oak)": we don't want explicit s in the pattern; please, have a look at er-shoaib's answer

                                – Dmitry Bychenko
                                Nov 22 '18 at 11:42







                                "oak" is a counter example for @"s(oak)": we don't want explicit s in the pattern; please, have a look at er-shoaib's answer

                                – Dmitry Bychenko
                                Nov 22 '18 at 11:42




















                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429394%2fc-sharp-indexof-when-word-is-part-of-another-word-how-to%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                鏡平學校

                                ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                                Why https connections are so slow when debugging (stepping over) in Java?