您如何区分运算符与知道它不是运算符但属于字符串?
How do you differentiate an operator vs know it is not operator but belong to a string?
例如这是字符串:
"Hello, this is challenging\n" + "you think it is easy?\n" + 变量名 + " 3 + 4 = 7\n"
Dim example = """Hello, this is challenging\n"" + ""you think it is easy?\n"" + variableName + "" 3 + 4 = 7\n"""
我想用编程的方式把字符串排列成:
"Hello, this is challenging" + 换行符 + "you think it is easy?" + 换行符 + 变量名 + " 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"
标准方法是遍历字符串,计算 chars/flipping 一个布尔值,指示您是否在字符串中
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
这条单线适合我:
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")
根据你的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 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
。
例如这是字符串:
"Hello, this is challenging\n" + "you think it is easy?\n" + 变量名 + " 3 + 4 = 7\n"
Dim example = """Hello, this is challenging\n"" + ""you think it is easy?\n"" + variableName + "" 3 + 4 = 7\n"""
我想用编程的方式把字符串排列成:
"Hello, this is challenging" + 换行符 + "you think it is easy?" + 换行符 + 变量名 + " 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"
标准方法是遍历字符串,计算 chars/flipping 一个布尔值,指示您是否在字符串中
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
这条单线适合我:
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")
根据你的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 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
。