locked
How can i remove/delete digits in the beginning of a string ? RRS feed

  • Question

  • The problem is that in tn.Text the string start with a number and a dot/point for example:

    1. hello
    2. hi
    3. world

    And i need that tn.Text will contain only the text for example:

    hello
    hi
    world

    private void FindRecursive(TreeNode treeNode)
                {
                    foreach (TreeNode tn in treeNode.Nodes)
                    {
                        if (tn.Text == this.txtNodeTextSearch.Text)
                            tn.BackColor = Color.Yellow;
        
                        FindRecursive(tn);
                    }
                }


    Tuesday, July 29, 2014 5:35 PM

Answers

  • You can put metadata in the Tag of the TreeNode.  Why not preprocess all the nodes and stick the search criteria matching string (the string with the "1." trimmed off.  e.g.: just "hello") in the Tag.  Then you don't have to recompute it all the time.
    • Marked as answer by Chocolade1972 Wednesday, July 30, 2014 7:37 AM
    Tuesday, July 29, 2014 8:06 PM

All replies

  • Hi!

    You could do that using Substring(),  or even could create a Regex that find then replaces every special character and number from a string.

    String result = tx.Text.Substring(2);


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Tuesday, July 29, 2014 5:57 PM
  • If the number is always followed by '.' you could use String Split() and just look at the second element in the array. You could split on the space if the '.' is always followed by space.

    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Tuesday, July 29, 2014 5:57 PM
  • You can even try extension functions:

    string sample = "2. hi";
    
    string result =
            string.Concat(
                sample
                    .SkipWhile( char.IsWhiteSpace )
                    .SkipWhile( char.IsDigit )
                    .SkipWhile( ( c, i ) => i == 0 && c == '.' )
                    .SkipWhile( char.IsWhiteSpace ) );
    
    

    Tuesday, July 29, 2014 7:29 PM
  • The problem is that in tn.Text the string start with a number and a dot/point for example:

    1. hello
    2. hi
    3. world

    And i need that tn.Text will contain only the text for example:

    hello
    hi
    world

    Your example has a space after each dot. Is it always there?

    Is that supposed to be part of the string you want?

    string s1 = "1. hello";
    string s1a = s1.Substring(s1.IndexOf('.') + 1); // Gives s1a as " hello"
    

    If you want to remove the leading space(s):

    s1a = s1a.TrimStart(" ".ToCharArray());
    

    Or you can just use TrimStart to remove the numbers, dot and any spaces:

    // Gives s2a as "hello"
    string s2 = "1. hello";
    string s2a = s2.TrimStart("0123456789. ".ToCharArray());
    

    - Wayne

    Tuesday, July 29, 2014 7:38 PM
  • Another approach is to use a regular expression.
    string result = Regex.Replace( input, @"^\d*\.\s*", string.Empty );

    Tuesday, July 29, 2014 7:39 PM
  • This is what i tried to do now this is the completed class code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace ScrollLabelTest
    {
        public partial class DisplayResponses : Form
        {        
            private List<string> nodesNames = new List<string>();
            private List<TreeNode> CurrentNodeMatches = new List<TreeNode>();
    
            public DisplayResponses()
            {
                InitializeComponent();
    
                addmore();
            }
    
            public void addmore()
            {
                foreach (List<string> l_branch in ListsExtractions.responsers)
                {
                    TreeNode l_node = treeView1.Nodes.Add(l_branch[l_branch.Count - 1]);
    
                    for (int l_count = 0; l_count < l_branch.Count - 1; l_count++)
                    {
                        l_node.Nodes.Add(l_branch[l_count]);
                    }
                }
            }
    
            private void DisplayResponses_Load(object sender, EventArgs e)
            {
    
            }
    
            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                try
                {
                    txtName.Text = "";
                    txtParentName.Text = "";
                    txtText.Text = "";
                    txtTag.Text = "";
    
                    if (treeView1.SelectedNode.Name != null)
                    {
                        txtName.Text = treeView1.SelectedNode.Name.ToString();
                    }
                    if (treeView1.SelectedNode.Text != null)
                    {
                        txtText.Text = treeView1.SelectedNode.Text.ToString();
                    }
                    if (treeView1.SelectedNode.Tag != null)
                    {
                        txtTag.Text = treeView1.SelectedNode.Tag.ToString();
                    }
                    if (treeView1.SelectedNode.Parent != null)
                    {
                        txtParentName.Text = treeView1.SelectedNode.Parent.Text.ToString();
                    }
                }
                catch { }
            }
    
            private void treeView1_Click(object sender, EventArgs e)
            {
                ClearBackColor();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                ClearBackColor();
                try
                {
                    TreeNode[] tn = treeView1.Nodes[0].Nodes.Find(txtNodeSearch.Text, true);
                    for (int i = 0; i < tn.Length; i++)
                    {
                        treeView1.SelectedNode = tn[i];
                        treeView1.SelectedNode.BackColor = Color.Yellow;
                    }
                }
                catch { }
            }
    
            private void ClearBackColor()
            {
                TreeNodeCollection nodes = treeView1.Nodes;
                foreach (TreeNode n in nodes)
                {
                    ClearRecursive(n);
                }
            }
    
            private void ClearRecursive(TreeNode treeNode)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    tn.BackColor = Color.White;
                    ClearRecursive(tn);
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                ClearBackColor();
                FindByText();
            }
    
            private void FindByText()
            {
                TreeNodeCollection nodes = treeView1.Nodes;
                foreach (TreeNode n in nodes)
                {
                    FindRecursive(n);
                }
            }
    
            private void FindRecursive(TreeNode treeNode)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    int index = tn.Text.IndexOf(" ");
                    string text = tn.Text.Substring(0, index);
                    tn.Text = tn.Text.Replace(text, "").TrimStart();
                    if (tn.Text == this.txtNodeTextSearch.Text)
                        tn.BackColor = Color.Yellow;
    
                    FindRecursive(tn);
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                ClearBackColor();
                FindByTag();
            }
    
            private void FindByTag()
            {
                TreeNodeCollection nodes = treeView1.Nodes;
                foreach (TreeNode n in nodes)
                {
                    FindRecursiveTag(n);
                }
            }
    
            private void FindRecursiveTag(TreeNode treeNode)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    if (tn.Tag.ToString() == this.txtTagSearch.Text)
                        tn.BackColor = Color.Yellow;
    
                    FindRecursiveTag(tn);
                }
            } 
        }
    }

    I added this part in the method FindRecursive:

    int index = tn.Text.IndexOf(" ");
    string text = tn.Text.Substring(0, index);
    tn.Text = tn.Text.Replace(text, "").TrimStart();

    When i'm using a breakpoint i see it does removing the digits and point as i wanted.

    If tn.Text was: 4. hello

    So now it's: hello

    The problem is that when i added this part of code everything got very slow taking forever.

    If i remove this part of code with the IndexOf and Substring it's working fast but then i need to type in textBox2 also the digits: 4. hello

    And not only hello as i wanted.

    Tuesday, July 29, 2014 7:59 PM
  • You could just find the index of the dot and use the String.Substring method:

    string s = "1. hello";
    string t = s.Substring(s.IndexOf('.') + 1).Trim(); //= hello
    
    private void FindRecursive(TreeNode treeNode)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    //tn.Text = "1. hello"
                    string x = tn.Text.Substring(tn.Text.IndexOf('.') + 1).Trim(); //= hello
                    if (x == this.txtNodeTextSearch.Text)
                        tn.BackColor = Color.Yellow;
    
                    FindRecursive(tn);
                }
            }


    Tuesday, July 29, 2014 8:01 PM
  • You can put metadata in the Tag of the TreeNode.  Why not preprocess all the nodes and stick the search criteria matching string (the string with the "1." trimmed off.  e.g.: just "hello") in the Tag.  Then you don't have to recompute it all the time.
    • Marked as answer by Chocolade1972 Wednesday, July 30, 2014 7:37 AM
    Tuesday, July 29, 2014 8:06 PM
  • I did this:

    private void FindRecursive(TreeNode treeNode)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    string result = Regex.Replace(tn.Text, @"^\d*\.\s*", string.Empty);
                    if (tn.Text == this.txtNodeTextSearch.Text)
                        tn.BackColor = Color.Yellow;
    
                    FindRecursive(tn);
                }
            }

    This is working but i have another problem.

    tn.Text contain the node name. And i want to make two different things:

    This is how the main treeView1 look like:

    When i type a string in the : Node Search (By Text) and click Find i want it to highlight all the words that any of the nodes names contain it. Not to highlight/color the whole node name but only the word i clicked in the textBox2. 

    For example if i typed in the textBox2 (By Text) the word: hello and there is a node name: hello world

    The color in yellow only the word hello

    And if there are any other node names that contain hello color only the word hello.

    And same thing behaviour when i click on node '+' if i expanded a node and now i type a word in the textBox2 and click Find this time the search should search only in the expanded node only in the child/sub nodes and highlight again only the places that contain the word i typed in the textBox2.

    This is how it look like when i click on the '+' of the first node:


    Tuesday, July 29, 2014 8:20 PM
  • You can put metadata in the Tag of the TreeNode.  Why not preprocess all the nodes and stick the search criteria matching string (the string with the "1." trimmed off.  e.g.: just "hello") in the Tag.  Then you don't have to recompute it all the time.

    Wyck but how can i Tag all the nodes ? One by one it will take a long time.

    So far i didn't give any of the nodes a Tag name not a Name and not a Parent Name.

    They are all have only Text.

    You can see in the screenshots i added that in the Information on the right top only the Text field is filled when i click on a node.

    Is there any way to give a tag automatic to all nodes ? I didn't understand this part of the AfterSelect event.

    Tuesday, July 29, 2014 8:24 PM
  • How did you add the TreeNodes in the first place?
    Wednesday, July 30, 2014 1:55 AM
  • To handle this situation, you can simply create a new Substring of the original string. Which is, to remove first 3 characters, first number second period third the white space. This way you can have everything in the String, but not the number and the period and the white space.

    String newTxt = tx.Text.Substring(2);
    In this code, the new string starts at the 3rd character and then continues to the end of the String. You can read the MSDN document for this: http://msdn.microsoft.com/en-us/library/hxthx5h6(v=vs.110).aspx

    ~!Firewall!~

    Wednesday, July 30, 2014 2:53 AM
  • How did you add the TreeNodes in the first place?

    Wyck in the top of the class the method addmore:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    
    namespace ScrollLabelTest
    {
        public partial class DisplayResponses : Form
        {        
            private List<string> nodesNames = new List<string>();
            private List<TreeNode> CurrentNodeMatches = new List<TreeNode>();
    
            public DisplayResponses()
            {
                InitializeComponent();
                
                addmore();
            }
    
            public void addmore()
            {
                foreach (List<string> l_branch in ListsExtractions.responsers)
                {
                    TreeNode l_node = treeView1.Nodes.Add(l_branch[l_branch.Count - 1]);
    
                    for (int l_count = 0; l_count < l_branch.Count - 1; l_count++)
                    {
                        l_node.Nodes.Add(l_branch[l_count]);
                    }
                }
            }
    
            private void DisplayResponses_Load(object sender, EventArgs e)
            {
    
            }
    
            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                try
                {
                    txtName.Text = "";
                    txtParentName.Text = "";
                    txtText.Text = "";
                    txtTag.Text = "";
    
                    if (treeView1.SelectedNode.Name != null)
                    {
                        txtName.Text = treeView1.SelectedNode.Name.ToString();
                    }
                    if (treeView1.SelectedNode.Text != null)
                    {
                        txtText.Text = treeView1.SelectedNode.Text.ToString();
                    }
                    if (treeView1.SelectedNode.Tag != null)
                    {
                        txtTag.Text = treeView1.SelectedNode.Tag.ToString();
                    }
                    if (treeView1.SelectedNode.Parent != null)
                    {
                        txtParentName.Text = treeView1.SelectedNode.Parent.Text.ToString();
                    }
                }
                catch { }
            }
    
            private void treeView1_Click(object sender, EventArgs e)
            {
                ClearBackColor();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                ClearBackColor();
                try
                {
                    TreeNode[] tn = treeView1.Nodes[0].Nodes.Find(txtNodeSearch.Text, true);
                    for (int i = 0; i < tn.Length; i++)
                    {
                        treeView1.SelectedNode = tn[i];
                        treeView1.SelectedNode.BackColor = Color.Yellow;
                    }
                }
                catch { }
            }
    
            private void ClearBackColor()
            {
                TreeNodeCollection nodes = treeView1.Nodes;
                foreach (TreeNode n in nodes)
                {
                    ClearRecursive(n);
                }
            }
    
            private void ClearRecursive(TreeNode treeNode)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    tn.BackColor = Color.White;
                    ClearRecursive(tn);
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                ClearBackColor();
                FindByText();
                treeView1.Invalidate();
            }
    
            private void FindByText()
            {
                TreeNodeCollection nodes = treeView1.Nodes;
                foreach (TreeNode n in nodes)
                {
                    FindRecursive(n);
                }
            }
    
            private void FindRecursive(TreeNode treeNode)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    string result = Regex.Replace(tn.Text, @"^\d*\.\s*", string.Empty);
                    if (tn.Text == this.txtNodeTextSearch.Text)
                        tn.BackColor = Color.Yellow;
    
                    FindRecursive(tn);
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                ClearBackColor();
                FindByTag();
            }
    
            private void FindByTag()
            {
                TreeNodeCollection nodes = treeView1.Nodes;
                foreach (TreeNode n in nodes)
                {
                    FindRecursiveTag(n);
                }
            }
    
            private void FindRecursiveTag(TreeNode treeNode)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    if (tn.Tag.ToString() == this.txtTagSearch.Text)
                        tn.BackColor = Color.Yellow;
    
                    FindRecursiveTag(tn);
                }
            }
    
            private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
            {
                string texts = txtNodeTextSearch.Text;
                if (txtNodeTextSearch.Text != "")
                {
                    using (Font font = new Font(this.Font, FontStyle.Regular))
                    {
                        using (Brush brush = new SolidBrush(Color.Red))
                        {
                            e.Graphics.DrawString(texts, font, brush, e.Bounds.Left, e.Bounds.Top);
                        }
    
                        using (Brush brush = new SolidBrush(Color.Blue))
                        {
                            SizeF s = e.Graphics.MeasureString(texts, font);
                            e.Graphics.DrawString(texts, font, brush, e.Bounds.Left + (int)s.Width, e.Bounds.Top);
                        }
                    }
                }
            } 
        }
    }


    This method:

    public void addmore()
            {
                foreach (List<string> l_branch in ListsExtractions.responsers)
                {
                    TreeNode l_node = treeView1.Nodes.Add(l_branch[l_branch.Count - 1]);
    
                    for (int l_count = 0; l_count < l_branch.Count - 1; l_count++)
                    {
                        l_node.Nodes.Add(l_branch[l_count]);
                    }
                }
            }

    This method build the first screenshot i uploaded here .

    It's creating all the TreeNodes.

    responsers is List<List<string>> type

    I'm waiting for the List responsers to be created and then i click in form1 on strip menu and that make an instance for the class DisplayResponses and addmore create the TreeNodes.


    Wednesday, July 30, 2014 3:27 AM
  • Didn't you originally scrap this text from a webpage?  Doesn't the text still contain the tags?  See webpage below.  It is in Java but it will give a better idea of what you need to do.

    http://codereview.stackexchange.com/questions/6347/better-find-and-highlight-implementation-in-html-element


    jdweng

    Wednesday, July 30, 2014 5:46 AM