CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2012
    Posts
    36

    String Remove Question

    Hello,

    I'm trying to remove these two char's from this string and it has many different user names, so this data changes, but the last two KB will never change.

    I currently have it removing only the text within () and now I need to remove the KB

    Can you see where I'm making my mistake.

    Code:
    //string s = "User name (2048.0 KB)";
                string s = textBox1.Text.ToString();
    
                int start = s.IndexOf("(") + 1;
                int end = s.IndexOf(")", start);
                string result = s.Substring(start, end - start);
    
                result.Remove(0, 2);
    
                textBox2.Text = result;
    Thanks,

  2. #2
    Join Date
    May 2012
    Posts
    36

    Re: String Remove Question

    I figure it out
    string result2 = result1.Remove((result1.Length -2),2);

    Thanks

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: String Remove Question

    The easiest way to do this is to step through the code in a debugger and look at each line as it is processed.

    If I read things correctly, I think you are trying to extract the number out of the string (e.g. "User name (2048.0 KB)" => "2048.0").

    If so, change your end index to
    int end = s.LastIndexOf(" ");

    and remove the result.Remove(0, 2); call

    Code:
    //string s = "User name (2048.0 KB)";
    string s = textBox1.Text.ToString();
    
    int start = s.IndexOf("(") + 1;
    int end = s.LastIndexOf(" ");
    
    string result = s.Substring(start, end - start);
    
    textBox2.Text = result;
    If this isn't it, please post the a couple of examples of what the string looks like before and after.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured