0

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"
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Gin May
  • 43
  • 1
  • 8
  • Please provide your example string and your expected output as valid VB.NET code. – Enigmativity May 11 '20 at 23:30
  • For example, is `Dim example = """Hello, this is challenging\n"" + ""you think it is easy?\n"" + variableName + "" 3 + 4 = 7\n"""` correct? – Enigmativity May 11 '20 at 23:34
  • yes you are right, I think you got the idea, too many escape quotation, that is why I didn't add it, thank you for the help – Gin May May 11 '20 at 23:46
  • No, seriously. Can you please put valid VB.NET code in your question? It's ambiguous until you do. The "string becomes" string looks wrong to me. I would like compilable code to be able to work from and to compare my result. – Enigmativity May 11 '20 at 23:49
  • ok, I just verified, yes, that is correct – Gin May May 11 '20 at 23:57
  • Your string you posted originally as your output is different from your valid code. See why valid code is important? – Enigmativity May 12 '20 at 00:03
  • Please don't change your question **AFTER** you get valid answers. You should only ever add to your question if there are any other things you missed. – Enigmativity May 12 '20 at 00:17
  • I re-edited your question to show you how to do so. You may want to check if you need to add anything else. – Enigmativity May 12 '20 at 00:18
  • why do you use "Function(x, n) If(n Mod 2 = 1" I dun understand how do Mod helped – Gin May May 12 '20 at 00:30
  • The use of `Mod` was to ensure that we only replaced the `\n` inside double-quotes. – Enigmativity May 13 '20 at 01:34

2 Answers2

1

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.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • I wish to try but I never seen this method before, it says "System.InvalidCastException: 'Unable to cast object of type 'd__7`2[System.String,System.String]' to type 'System.String[]'.'" – Gin May May 12 '20 at 00:07
  • I try to run every answer that I post, and in this case this is no exception. It ran perfectly fine for me. I reconfirmed. Here's a [fiddle](https://dotnetfiddle.net/6VXhtz) of the code working. – Enigmativity May 12 '20 at 00:10
  • Wow you are a genius. I am so impressed you able to solve it that quick. Do you mind explain to me what your code do? – Gin May May 12 '20 at 00:25
  • I've added some explanation. – Enigmativity May 12 '20 at 00:38
  • owh I never learn or know lambda select exist at all. Wow that is so amazing. How can you know it is always odd? Won't the occurrence of odd happens in even for example? then what should we do? – Gin May May 12 '20 at 00:45
  • The `n Mod 2 = 1` ensures that we only change the value on the odd lines. – Enigmativity May 12 '20 at 00:49
  • @ginmay `Select` is part of Linq. If you do any significant work in .NET (VB or otherwise), you'd be well advised to learn the basics of Linq. If you want an in-depth review that gets into some of the nuts and bolts of how it works, Jon Skeet's EduLinq series is great. – Craig May 12 '20 at 14:26
0

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
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • look at number 2 of the problem I stated, detecting is easy. But filter it out is not – Gin May May 11 '20 at 23:49
  • I change the message depending on whether I find the operator inside or outside a string - so just change the behaviour (don't "filter it out" if you are "inside" a string) – Caius Jard May 12 '20 at 06:37