locked
StringBuilder Replace RRS feed

  • Question

  • User-1066344940 posted

    Im using a value based on a condition to fill a txtBox.

    var strControlNum As New StringBuilder()
                strControlNum.Append(ctrlNum.ToString())
    			strControlNum.Append(" : " + txtProdChange.Value)
    			txtProdChange.Value = strControlNum.ToString()

    Is there a StringBuilder.Replace() I could use that would replace anything before the ":" when it a different value? My issue is this. I am getting a ID back from SQL and appending it to the text - txtProdChange.Value, in order to create a title. I have error trapping set up, but on a post back, when theres an error on my page, the old value still shows. Example:

    88S : 87S : Test Clear and Checkbox

    I only want the 88S. I don't want to completely clear the Text box and have the user type the words "Test Clear and Checkbox" again. I just would like to replace whatevers in fron t of the ":" with the new value. Any suggestions. I tried several ways, but I believe that Stringbuilder has to have a method for something like this.

    Thursday, April 2, 2015 3:53 PM

Answers

All replies

  • User-821857111 posted

    The StringBuilder class doesn't have a Replace method (it wouldn't make sense to have it), but the String type has. I get code blindness when reading variable names composed of meaningless abbreviations so it's far from clear to me what your requirement is, but I'm sure it can be solved with some kind of simple if clause and string.Replace.

    Thursday, April 2, 2015 4:08 PM
  • User-1066344940 posted

    Oh, well thank you very much for the input. Just following the "naming convention" that's required of me. Didn't mean to offend you by following the rules required of me.  I just need to replace the string before the semi colon if its different than the last time. I wasn't sure how to go about it.

    Thursday, April 2, 2015 4:56 PM
  • User-434868552 posted

    @Mark_F

    if i understand your question correctly, your problem text lies within

    txtProdChange.Value

    so you could simply use the String.LastIndexOf to search for the colon; if you find a colon, the use a Sting.Substring to grab the text you require from the backend of txtProdChange.Value just dropping your 87S:

    MSDN references:

    https://msdn.microsoft.com/en-us/library/system.string.lastindexof(v=vs.110).aspx 

    https://msdn.microsoft.com/en-us/library/system.string.substring(v=vs.110).aspx 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 3, 2015 12:27 AM
  • User-821857111 posted

    I assume that the "87S: " is prepended in a previous operation? If so you can use Regex.Replace to remove the pattern. The pattern below will match a pattern found from the beginning of the string that consists 1 or more numbers, followed by 1 or more capital letters followed by a space, then a colon, then a space:

    ^\d+[A-Z]+\s\:\s

    And the following operation will replace that with an empty string, effectively removing it:

    strControlNum.Append(" : " + Regex.Replace(txtProdChange.Value, "^\d+[A-Z]+\s\:\s", string.Empty))

    Didn't mean to offend you by following the rules required of me.

    Erm, yes. Perhaps my views on your naming convention weren't relevant or helpful. My apologies.

    Friday, April 3, 2015 5:12 AM
  • User-1066344940 posted

    Mike, thank you. this is a very "clean " solution. Yes, "87S: " is prepended in a previous operation. There is never a space after the capital letter. So, Should I modify this to be:

    ^\d+[A-Z]+\:
    strControlNum.Append(" : " + Regex.Replace(txtProdChange.Value, "^\d+[A-Z]+\:", string.Empty))

    Also, apology accepted. no worries.

    Friday, April 3, 2015 7:01 AM
  • User-821857111 posted

    There is never a space after the capital letter. So, Should I modify this to be:

    ^\d+[A-Z]+\:

    Yes. You can check what the metacharacters represent by referring to my cheat sheet: http://www.mikesdotnetting.com/article/46/c-regular-expressions-cheat-sheet

    Friday, April 3, 2015 7:52 AM
  • User-1066344940 posted

    Hi Mike,

    Thanks for the input but this regex did not work. I tried it the way thta you wrote it, and I tried it with my variation. It didnt replace my string. I am looking at using the SubString methid instead. Thanks allot though for taking the time to give me input.

    Monday, April 6, 2015 1:06 PM
  • User-434868552 posted

    @Mark_F

    i like to repeat Paul Linton ... if you need to use regex to solve a problem, you likely have two problems.

    You string is simple ... it's a perfect candidate for .NET Framework string methods.

    Have you tried my suggestion above, here:  http://forums.asp.net/post/5888697.aspx 

    Monday, April 6, 2015 1:12 PM
  • User-1066344940 posted

    Yes, it does lie within that textbox. I have tried the methods that you suggested. I m close to success. The problem is, when I use LastIndexOf() it requires that I pass an int representing the position of the character, or its index position, which is never the same value. But, you have given me a starting point. Thank you very much.

    Monday, April 6, 2015 5:57 PM
  • User-1066344940 posted

    This is what I actually ended up doing:

    Dim prodChange As String = txtProdChange.Value
            Dim s As String = prodChange.Substring(prodChange.IndexOf(":") + 1)
            Dim strControlNum As New StringBuilder()
    		If (DirectCast(Session("controlID"), String) IsNot Nothing) OrElse (DirectCast(Session("controlID"), String) <> "") Then
    			strControlNum.Append(Session("controlID").ToString())
    			strControlNum.Append(" : ")
                strControlNum.Append(s)
    			txtProdChange.Value = strControlNum.ToString()

    Works perfectly. Thanks for getting me started and pointing me in the right direction. 

    Tuesday, April 7, 2015 9:01 AM