Truncated Text using ToCharArray()

I have pulling the text from tree nodes and adding them to List<>.  I have a method that grabs the text from the 2nd node for database querying and 3rd node for visualiation in another control, it removes specific text "TreeNode: " from the 2nd node (n2), before adding it to the List.  The problem is, that it is truncating the text.  Is there something I should be using instead of ToCharArray()?
...
...
public static List<string> trimSubconcept = new List<string>();
 
private void btnSelectExp_Click(object sender, EventArgs e)
        {
            foreach (TreeNode n1 in treeView1.Nodes)
                foreach (TreeNode n2 in n1.Nodes)
                    foreach (TreeNode n3 in n2.Nodes)
                        if (n3.Checked)
                        {
                            lstbxItems.Items.Add(n3.Text);
 
                            List<string> subconcept = new List<string>();
                            string remove = "TreeNode: ";
                            string modSubconcept ="";
                            subconcept.Add(n2.ToString());
                            foreach(string str in subconcept)
                            modSubconcept = str.Trim(remove.ToCharArray());
                            trimSubconcept.Add(modSubconcept);
                        }

Open in new window

MichelleLacyAsked:
Who is Participating?
 
MichelleLacyConnect With a Mentor Author Commented:
I figured out the solution using the following code:

List<string> subconcept = new List<string>();
string modSubconcept ="";
subconcept.Add(n2.ToString());
foreach (string str in subconcept)
modSubconcept = str.Remove(0, 9).Trim();
trimSubconcept.Add(modSubconcept);
0
 
mokuleCommented:
try
modSubconcept = str.Remove(remove,"");
0
 
trunghieubkitConnect With a Mentor Commented:
using

modSubconcept = str.Replace(remove, "");


your solution have some mistakes
if  str = "TreeNode: eeeerrrTTTTNNNNNoo" or some strings like this
=> modSubconcept = ""

0
 
MichelleLacyAuthor Commented:
The string I want to remove will always be the same.  Is there a way to search for the entire substring "TreeNode: " without breaking it into characters?  Trunghieubkit you are right.  If any of the characters in "TreeNode: " appear in text that I want to keep, it will be removed.  And that is exactly what is happening.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.