2

I was looking at some code from a co-worker and it looks as though they're using a Char Array in their String.Replace:

txtPAPTransit.Text = g_aAllBranches(instApplication.branchForCumisReporting.ToLower).transit.ToString.Replace({" "c, "-"c}, "").Substring(3)

Which doesn't remove the "-" from the string in the textbox (the string being 123-45678). When I try it making the Substring as 4 instead of 3, it works. Which strikes me as very odd, why would it behave like this in this case when the index is 0 based?

When I write the replace like this it works fine:

txtPAPTransit.Text = g_aAllBranches(instApplication.branchForCumisReporting.ToLower).transit.ToString.Replace("-"c, "").Substring(3)

Afterwards the string appears as 45678, which is the correct result.

Why would it work in the case of replacing the substring with 4 as I mention above? And why does it have a problem with the char array?

6
  • Can you give us a sample value so we can follow along? Commented Mar 15, 2016 at 20:16
  • Why would you expect the first example to remove the "-" from the textbox? The first argument is what to remove, in that case, it would be the space that would be replaced with the - Commented Mar 15, 2016 at 20:19
  • @ScottMarcus But if you notice the .Replace has a char array in its 'oldValue' position. .Replace({" "c, "-"c,"") which if the logic were correct, would mean that it would replace either a whitespace or a dash with nothing. But the Replace doesn't seem to be able to handle this. Commented Mar 15, 2016 at 20:23
  • 2
    it doesnt remove the "-" because the Replace usage is wrong. {" "c, "-"c} will equate to " " + " -" not act as a multiple replace for any space and any "-". Thus nothing is removed. Replace takes a single char or a string: an array of char is a string so nothing happens unless it is "123 -45678". .Replace(" "c, "").Replace("-"c, "") would be the way to do both/either Commented Mar 15, 2016 at 20:24
  • 1
    Tricky code for no reason. Your coworker seems to be tired of VB Commented Mar 15, 2016 at 20:27

1 Answer 1

3

The Replace usage is not correct - or your co-workers understanding of it is flawed. The 2 overloads are:

- String.Replace(Char, Char)  
- String.Replace(String, String)

So, Replace({" "c, "-"c}, "") will not act to replace any space and any dash with String.Empty. Instead, since a char array is a string, it will look to replace " -" (space+dash) as in "123 -45678".

The correct way to replace either would be:

Dim sample = "123- ABCDEFG"     
newTxt = sample.Replace(" "c, "").Replace("-"c, "")

"123ABCDEFG"

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help! I've got it written the proper way with the 2 Replace functions and it works great now!
Glad to help - that long, long expression in the Q makes it hard to follow wothout an example because the "problem part" is scrolled off

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.