vb.net

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


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"

Solution

  • 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.