Replacing Mid with Middle in R2

I have code that adds a space after a period, if one does not exist. (ta is a textarea)
(sometext.sometext to some text. sometext)

This code in 2019r1.1 and 2019r2 works
For q As Integer = 0 To Len(ta.Text) - 1
If ta.Text.Mid(q, 1) = “.” And ta.Text.Mid(q+ 1, 1) <> Chr(32) Then
ta.Text = Replace( ta.Text, ta.Text.Mid(q, 2), ". "+ Right(ta.Text.Mid(q, 2), 1) )
End If

This code in 2019r2 does not work
For q As Integer = 0 To ta.Value.Length - 1
If ta.Value.Middle(q, 1) = “.” And ta.Value.Middle(q+ 1, 1) <> Chr(32) Then
ta.Value = Replace( ta.Value, ta.Value.Middle(q, 2), ". "+ ta.Value.Middle(q, 2).Right( 1) )
End If

If I replace Middle with Mid in line 2 and 3 in the R2 code it works. What is the problem with Middle?

The Mid function is 1-based, whereas Middle is zero-based. So maybe remove the q+1 in your If statement, when using Middle?

Hope that helps.

nothing BUT Middle is NOT an exact replacement for MID (as Scott notes)
you also have to adjust the parameters
mid was 1 based
middle is 0 based

so your old code needs adjusting and NOT just replacing MID with MIDDLE

I expect to see a LOT of this in projects as they are updated as so many things changed to being 0 based instead of 1 based

The relevant entry in the LR is:

mid was 1 based
middle is 0 based [broken heart]

The LR said:

The first character is numbered 0. :wink:

For the sake of help, here is an API2 function to do the intended processing.

You can use it in your example as ta.Value = DotSpacing(ta.Value)

Public Function DotSpacing(txt As String) as String Var orig() As String = txt.Split("") Var res() As String For i As Integer = 0 To orig.LastRowIndex If i < orig.LastRowIndex And orig(i) = "." And orig(i+1) <> " " Then res.AddRow ". " // stream a dot + space Else res.AddRow orig(i) // stream the char End Next Return String.FromArray(res, "") // I wish it was a consistent: res.Join("") End Function

I don’t know your use case, but have in mind that something like “I was there.He paid $7.00 for a coffee.” becomes “I was there. He paid $7. 00 for a coffee.”

Thanks everybody. I use middle in several other places and the difference didn’t cause a problem. I will now make the necessary changes.

i like your API2 example and will use it, also the text I’m using this code on will not have the $7.00 problem.

there are many chnages like this where things moved from being 1 based to 0 based
you need to review all the chnages to make sure you have handled these properly

*** Never mind! , I see in the fine print it’s an r3 item. I guess the take away here is that r2 Analysis is only a suggestion (that might not work)

MidB is supposed to be replaced with MiddleBytes, but right now r2 compiler is claiming that no such method exists; and yet here it is documented:

The example simply doesn’t work. Yet r2 Analysis says I should update it

Am I missing something???

the irony ?>/sarcasm>

no I dont think you are

Since when is the documentation updated with versions that haven’t been released?

or replace ". " into “.” and replace “.” to ". " :slight_smile:

This is a good time to use a regular expression.

dim rx as new RegEx
rx.SearchPattern = "\\.(?!\\x20|$)"
rx.ReplacementPattern = ". "
rx.Options.ReplaceAllMatches = true

s = rx.Replace( s )

This won’t add a space at the end of lines or the text.

Here is a pattern that takes care of the dot-as-decimal problem by ignoring dots that are followed by a digit, and also ones that are followed by another dot:


Sometimes it creeps in. I knew that R2 was going to include BeginsWith for strings, as String.BeginsWith started appearing in the documentation dropdown when I was entering String. This was earlier in the summer before R2 was released.