Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. string.IndexOf help

string.IndexOf help

Scheduled Pinned Locked Moved C#
help
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    solutionsville
    wrote on last edited by
    #1

    I want to take the value of textBox1.text, pass it to a find routine and then display the results of all lines that have the given text. The find portion I have figured out. the IndexOf routine I have not figured out. Here is my find routine; private void btnFind_Click(object sender, System.EventArgs e) { try { int StartPosition; StringComparison SearchType; if (chkMatchCase.Checked == true) { SearchType = StringComparison.Ordinal; } else { SearchType = StringComparison.OrdinalIgnoreCase; } StartPosition = mMain.rtbDoc.Text.IndexOf(txtSearchTerm.Text, SearchType); if (StartPosition == 0) { MessageBox.Show("String: " + txtSearchTerm.Text.ToString() + " not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } mMain.rtbDoc.Select(StartPosition, txtSearchTerm.Text.Length); mMain.rtbDoc.ScrollToCaret(); mMain.Focus(); btnFindNext.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error"); } } Lets say I want to search for and display every line that has the word "one" in it and then display it in a RichTextBox one plus four two plus four three plus four one minus four one divided by four two minus four two divided by four three plus five three minus five When the search is done, it show like this in the RTB with nothing else.; one plus four one minus four one divided by four Thanks in advance! Brian

    M J 2 Replies Last reply
    0
    • S solutionsville

      I want to take the value of textBox1.text, pass it to a find routine and then display the results of all lines that have the given text. The find portion I have figured out. the IndexOf routine I have not figured out. Here is my find routine; private void btnFind_Click(object sender, System.EventArgs e) { try { int StartPosition; StringComparison SearchType; if (chkMatchCase.Checked == true) { SearchType = StringComparison.Ordinal; } else { SearchType = StringComparison.OrdinalIgnoreCase; } StartPosition = mMain.rtbDoc.Text.IndexOf(txtSearchTerm.Text, SearchType); if (StartPosition == 0) { MessageBox.Show("String: " + txtSearchTerm.Text.ToString() + " not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } mMain.rtbDoc.Select(StartPosition, txtSearchTerm.Text.Length); mMain.rtbDoc.ScrollToCaret(); mMain.Focus(); btnFindNext.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error"); } } Lets say I want to search for and display every line that has the word "one" in it and then display it in a RichTextBox one plus four two plus four three plus four one minus four one divided by four two minus four two divided by four three plus five three minus five When the search is done, it show like this in the RTB with nothing else.; one plus four one minus four one divided by four Thanks in advance! Brian

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, I'm not sure if I understand you right, but if you have a multiline TextBox and want to iterate over the lines to find the lines with a search string in it. Then use a method like this: using System.Collections.Specialized; public static StringCollection FindLinesOverSearchString(string[] LinesToSearch, string TextToSearch) { StringCollection resultLines = new StringCollection(); if(LinesToSearch!=null) { foreach(string actLine in LinesToSearch) { if(actLine.IndexOf(TextToSearch)!=-1) { resultLines.Add(actLine); } } } return resultLines; } And call it like this: StringCollection textboxresult = FindLinesOverSearchString(yourTextBox.Lines, "searchstring");

      All the best, Martin

      S 1 Reply Last reply
      0
      • S solutionsville

        I want to take the value of textBox1.text, pass it to a find routine and then display the results of all lines that have the given text. The find portion I have figured out. the IndexOf routine I have not figured out. Here is my find routine; private void btnFind_Click(object sender, System.EventArgs e) { try { int StartPosition; StringComparison SearchType; if (chkMatchCase.Checked == true) { SearchType = StringComparison.Ordinal; } else { SearchType = StringComparison.OrdinalIgnoreCase; } StartPosition = mMain.rtbDoc.Text.IndexOf(txtSearchTerm.Text, SearchType); if (StartPosition == 0) { MessageBox.Show("String: " + txtSearchTerm.Text.ToString() + " not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } mMain.rtbDoc.Select(StartPosition, txtSearchTerm.Text.Length); mMain.rtbDoc.ScrollToCaret(); mMain.Focus(); btnFindNext.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error"); } } Lets say I want to search for and display every line that has the word "one" in it and then display it in a RichTextBox one plus four two plus four three plus four one minus four one divided by four two minus four two divided by four three plus five three minus five When the search is done, it show like this in the RTB with nothing else.; one plus four one minus four one divided by four Thanks in advance! Brian

        J Offline
        J Offline
        Justin Perez
        wrote on last edited by
        #3

        solutionsville wrote:

        Lets say I want to search for and display every line that has the word "one" in it and then display it in a RichTextBox

        Here's how you can use IndexOf() int hasOne; string strWorkString = string.Empty; strWorkString = "one plus 3"; hasOne = strWorkString.IndexOf("one"); That will report where in the string "one" is found. Also,

        solutionsville wrote:

        MessageBox.Show("String: " + txtSearchTerm.Text.ToString() + " not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

        When you do that, you should really use string.Format(). Do it like so: MessageBox.Show(string.Format("String: {0} not found", txtSearchTerm.Text.ToString()), "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

        I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

        1 Reply Last reply
        0
        • M Martin 0

          Hello, I'm not sure if I understand you right, but if you have a multiline TextBox and want to iterate over the lines to find the lines with a search string in it. Then use a method like this: using System.Collections.Specialized; public static StringCollection FindLinesOverSearchString(string[] LinesToSearch, string TextToSearch) { StringCollection resultLines = new StringCollection(); if(LinesToSearch!=null) { foreach(string actLine in LinesToSearch) { if(actLine.IndexOf(TextToSearch)!=-1) { resultLines.Add(actLine); } } } return resultLines; } And call it like this: StringCollection textboxresult = FindLinesOverSearchString(yourTextBox.Lines, "searchstring");

          All the best, Martin

          S Offline
          S Offline
          solutionsville
          wrote on last edited by
          #4

          Martin, if I understand you correctly, The method will find what I want. I am not sure I understand how I take the value of the textbox as a string and pass it to the above routine and then get it to the second textbox. I understand the string collextion piece, as it looks fairly simple in that respect. Thanks, Brian

          M 1 Reply Last reply
          0
          • S solutionsville

            Martin, if I understand you correctly, The method will find what I want. I am not sure I understand how I take the value of the textbox as a string and pass it to the above routine and then get it to the second textbox. I understand the string collextion piece, as it looks fairly simple in that respect. Thanks, Brian

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            solutionsville wrote:

            I am not sure I understand how I take the value of the textbox as a string and pass it to the above routine

            You don't need to do that. The method I posted, takes a string[] as parameter for the lines to search. The TextBox class has a property called "Lines" which is type of string[], and this will be passed as parameter. FindLinesOverSearchString(yourTextBox.Lines, "searchstring"); The second parameter is quit simple the string to search. The result is in my case a dynamic collection of the type: System.Collections.Specialized.StringCollection. This class has an "Add" method, which will be used to add the matching line to the collection. If you now have the result and want to show it in a second multiline textbox: //Again the method call for the search StringCollection resultLines = FindLinesOverSearchString(yourTextBox.Lines, "searchstring"); //create a new instance of string[], with the needed lenght string[] resultAsStringArray = new string[resultLines.Count]; //copy data of StringCollection to the new string[] resultLines.CopyTo(resultAsStringArray,0); //path it to the result TextBox "Lines" property yourTextBoxResult.Lines = resultAsStringArray; Hope it helps!

            All the best, Martin

            S 1 Reply Last reply
            0
            • M Martin 0

              solutionsville wrote:

              I am not sure I understand how I take the value of the textbox as a string and pass it to the above routine

              You don't need to do that. The method I posted, takes a string[] as parameter for the lines to search. The TextBox class has a property called "Lines" which is type of string[], and this will be passed as parameter. FindLinesOverSearchString(yourTextBox.Lines, "searchstring"); The second parameter is quit simple the string to search. The result is in my case a dynamic collection of the type: System.Collections.Specialized.StringCollection. This class has an "Add" method, which will be used to add the matching line to the collection. If you now have the result and want to show it in a second multiline textbox: //Again the method call for the search StringCollection resultLines = FindLinesOverSearchString(yourTextBox.Lines, "searchstring"); //create a new instance of string[], with the needed lenght string[] resultAsStringArray = new string[resultLines.Count]; //copy data of StringCollection to the new string[] resultLines.CopyTo(resultAsStringArray,0); //path it to the result TextBox "Lines" property yourTextBoxResult.Lines = resultAsStringArray; Hope it helps!

              All the best, Martin

              S Offline
              S Offline
              solutionsville
              wrote on last edited by
              #6

              That explains it better. I will have to mull this over a little more before I get it to sink in and work the way we are talking about. Thanks for the help! Brian

              M 1 Reply Last reply
              0
              • S solutionsville

                That explains it better. I will have to mull this over a little more before I get it to sink in and work the way we are talking about. Thanks for the help! Brian

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #7

                You are wellcome!

                All the best, Martin

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups