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



示例是字符串:">
你好,这很有挑战性" + "你认为这很容易吗?" + 变量名称 + " 3 + 4 = 7">

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

我想用编程方法把字符串排列成:">
你好,这很有挑战性" + 换行符 + "你认为这很容易吗?" + 换行符 + 变量名称 + " 3 + 4 = 7" + 换行

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

所以如您所见,它涉及获取引号中的文本
所以我在想:
1. 使用正则表达式获取报价,但如您所见,我们将省略变量
Name 2。我正在考虑使用 + 符号进行拆分,但如您所见,"3 + 4 = 7"中会出现误报

告诉我你怎么看,容易吗?还有其他步骤吗?


更新的示例和输出:

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

这个单行代码对我有用:

Dim example = """Hello, this is challengingn"" + ""you think it is easy?n"" + variableName + "" 3 + 4 = 7n"""
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 challengingn"" + ""you think it is easy?n"" + variableName + "" 3 + 4 = 7n"""
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")

我按照你的output2得到"Hello, this " + newline + "is challenging" + newline + "you think it is easy?" + newline + variableName + " 3 + 4 = 7" + newline.


以下是result2发生的事情:

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

所有双引号都已拆分。

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

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

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替换它们。

标准方法是遍历字符串,计算字符/翻转布尔值,以指示您是否在字符串内

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

相关内容

  • 没有找到相关文章

最新更新