您如何区分运算符vs知道它不是运算符而是属于字符串?

时间:2020-05-11 23:22:47

标签: vb.net vb.net-2010 vb.net-module

例如,这是字符串:
“您好,这很有挑战性\ n” +“您认为这很容易吗?\ n” + variableName +“ 3 + 4 = 7 \ n”

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

我想使用编程方法将字符串排列为:
“您好,这很具有挑战性” +换行符+“您认为这很简单?” +换行符+ variableName +“ 3 + 4 = 7” +换行符

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

如您所见,它涉及到将文本放在引号内
所以我在想:
1.使用正则表达式获取报价,但是如您所见,我们将省略variableName
2.我正在考虑使用+号进行拆分,但是如您所见,“ 3 + 4 = 7”

告诉我您的想法,这容易吗?还有其他步骤吗?


更新的示例和输出:

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 个答案:

答案 0 :(得分:1)

这种单线为我工作:

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")

我得到的与您的输出相同。


这是运行良好的更新示例:

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")

根据您的"Hello, this " + newline + "is challenging" + newline + "you think it is easy?" + newline + variableName + " 3 + 4 = 7" + newline,我得到output2


这是result2中发生的事情:

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

所有双引号都被分开。

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 + "", "" }

在每个奇数元素上,我们将\n替换为" + newline + "

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

然后使用"备份零件。

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

但是我们还有newline + ""形式的双引号,所以我们只用newline代替。

答案 1 :(得分:0)

标准方法是遍历字符串并计算字符数/翻转一个布尔值,以表明您是否在字符串中

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