How do you differentiate an operator vs know it is not operator but belong to a string?

89 Views Asked by At

Example this is the string:
"Hello, this is challenging\n" + "you think it is easy?\n" + variableName + " 3 + 4 = 7\n"

Dim example = """Hello, this is challenging\n"" + ""you think it is easy?\n"" + variableName + "" 3 + 4 = 7\n"""

I want to use programming approach to arrange the string becomes:
"Hello, this is challenging" + newline + "you think it is easy?" + newline + variableName + " 3 + 4 = 7" + newline

Dim output = """Hello, this is challenging"" + newline + ""you think it is easy?"" + newline + variableName + "" 3 + 4 = 7"" + newline"

So as you can see, it involves in getting the text inside quotation
So I am thinking:
1. use regex to get the quotation, but as you can see we will left out the variableName
2. I am thinking to split using + sign, but as you can see, there will be false positive in " 3 + 4 = 7"

tell me what do you think, is it easy? Is there another steps?


Updated example and output:

Dim example2 = """Hello, this \nis challenging\n"" + ""you think it is easy?\n"" + variableName + "" 3 + 4 = 7\n"""

Dim output2 = """Hello, this "" + newline + ""is challenging"" + newline + ""you think it is easy?"" + newline + variableName + "" 3 + 4 = 7"" + newline"
2

There are 2 best solutions below

7
On BEST ANSWER

This one-liner works for me:

Dim example = """Hello, this is challenging\n"" + ""you think it is easy?\n"" + variableName + "" 3 + 4 = 7\n"""
Dim output = """Hello, this is challenging"" + newline + ""you think it is easy?"" + newline + variableName + "" 3 + 4 = 7"" + newline"

Dim result = String.Join("""", example.Split(""""c).Select(Function(x, n) If(n Mod 2 = 1, x.Replace("\n", """ + newline"), x))).Replace("newline""", "newline")

I get the same as your output.


Here's the updated example working fine:

Dim example2 = """Hello, this \nis challenging\n"" + ""you think it is easy?\n"" + variableName + "" 3 + 4 = 7\n"""
Dim output2 = """Hello, this "" + newline + ""is challenging"" + newline + ""you think it is easy?"" + newline + variableName + "" 3 + 4 = 7"" + newline"
Dim result2 = String.Join("""", example2.Split(""""c).Select(Function(x, n) If(n Mod 2 = 1, x.Replace("\n", """ + newline + """), x))).Replace("newline + """"", "newline")

I get "Hello, this " + newline + "is challenging" + newline + "you think it is easy?" + newline + variableName + " 3 + 4 = 7" + newline as per your output2.


Here's what's going on in result2:

Dim splitOnQuotes = example2.Split(""""c)
'splitOnQuotes = { "", "Hello, this \nis challenging\n", " + ", "you think it is easy?\n", " + variableName + ", " 3 + 4 = 7\n", "" }

All of the double quotes are split out.

Dim replaceSlashNOnOddLines = splitOnQuotes.Select(Function(x, n) If(n Mod 2 = 1, x.Replace("\n", """ + newline + """), x))
'replaceSlashNOnOddLines = { "", "Hello, this " + newline + "is challenging" + newline + "", " + ", "you think it is easy?" + newline + "", " + variableName + ", " 3 + 4 = 7" + newline + "", "" }

On each odd element we replace \n with " + newline + ".

Dim joinOnQuotes = String.Join("""", replaceSlashNOnOddLines)
'joinOnQuotes = "Hello, this "" + newline + ""is challenging"" + newline + """" + ""you think it is easy?"" + newline + """" + variableName + "" 3 + 4 = 7"" + newline + """""

Then join back up theparts with ".

Dim result2 = joinOnQuotes.Replace("newline + """"", "newline")

But we had extra sets of double quotes in the form of newline + "", so we just replace these with newline.

2
On

The standard approach is to iterate over the string counting the chars/flipping a boolean that indicate if you're inside a string or not

Dim inSideAString = false
Dim quoteChar = "'"c
Dim escapeChar = "\"c
Dim lookForChar  "+"

Dim lastChar = " "c

For i = 0 to theString.Length - 1

  Dim c = theString(i)

  Dim prevChar = If(i > 0, theString(i-1), " "c) 'make it not the escape char

  If c = quoteChar AndAlso prevChar <> escapeChar Then 
    insideAString = (Not insideAString)
    Continue For
  End If

  If c = lookForChar Then
    If insideAString Then 
      Console.Write($"Found a {lookForChar} inside a string at position {i}!")
    Else
      Console.Write($"Found a {lookForChar} outside a string at position {i}!")
    End If
  End If

Next i