如何添加"代码"在一个字符串里面?

时间:2015-05-18 22:49:49

标签: vb.net quotes

所以,这里是我想要的字符串代码:

TEAM_SUPERADMIN = DarkRP.createJob("Superadmin on Duty", {
color = Color(0, 0, 0, 255),
model = {"models/player/group01/male_01.mdl"},
description = [[This is the Superadmin on Duty job!]],
weapons = {"weapon_fists", "unarrest_stick"},
command = "superadmin",
max = 5,
salary = 45,
admin = 0,
vote = false,
hasLicense = false,
})

我尝试将其放入文本框中是这样的:

TextBox14.Text = "TEAM_SUPERADMIN = DarkRP.createJob("Superadmin on Duty", {
color = Color(0, 0, 0, 255),
model = {"models/player/group01/male_01.mdl"},
description = [[This is the Superadmin on Duty job!]],
weapons = {"weapon_fists", "unarrest_stick"},
command = "superadmin",
max = 5,
salary = 45,
admin = 0,
vote = false,
hasLicense = false,
})"

但它认为字符串我是前两个引号内的文字。请帮忙!

3 个答案:

答案 0 :(得分:2)

您想要引用的每个地方都需要双引号。 “”Superadmin on Duty“”

如果您要将字符串放在多行上,则需要连续行。关闭字符串,然后在行的末尾添加_。然后使用& "more stuff..."

开始下一行

所以前几行似乎是:

TextBox14.Text = "TEAM_SUPERADMIN = DarkRP.createJob(""Superadmin on Duty"", {" & VbCrLf _
    & "color = Color(0, 0, 0, 255)," & VbCrLf _

还有其他几种方法可以做到这一点。您可以使用+ =继续向变量或控件添加文本:

TextBox14.Text = "TEAM_SUPERADMIN = DarkRP.createJob(""Superadmin on Duty"", {" & VbCrLf
TextBox14.Text += "color = Color(0, 0, 0, 255)," & VbCrLf
TextBox14.Text += "..."

您可以通过多种方式连接变量中的字符串,然后将控件的文本分配给变量的内容。请参阅Create a string and append text to it

答案 1 :(得分:2)

在字符串中用双引号替换双引号,并使用caraige返回构建字符串:

TextBox14.Text = "TEAM_SUPERADMIN = DarkRP.createJob(""Superadmin on Duty"", {" _
& vbLf & "color = Color(0, 0, 0, 255)," _
& vbLf & "model = {""models/player/group01/male_01.mdl""}," _
& vbLf & "description = [[This is the Superadmin on Duty job!]]," _
& vbLf & "weapons = {""weapon_fists"", ""unarrest_stick""}," _
& vbLf & "command = ""superadmin""," _
& vbLf & "max = 5," _
& vbLf & "salary = 45," _
& vbLf & "admin = 0," _
& vbLf & "vote = false," _
& vbLf & "hasLicense = false," _
& vbLf & "})"

请注意下划线后面的空格,我刚用它进行了测试,它可以正常工作:

MsgBox("TEAM_SUPERADMIN = DarkRP.createJob(""Superadmin on Duty"", {" _
& vbLf & "color = Color(0, 0, 0, 255)," _
& vbLf & "model = {""models/player/group01/male_01.mdl""}," _
& vbLf & "description = [[This is the Superadmin on Duty job!]]," _
& vbLf & "weapons = {""weapon_fists"", ""unarrest_stick""}," _
& vbLf & "command = ""superadmin""," _
& vbLf & "max = 5," _
& vbLf & "salary = 45," _
& vbLf & "admin = 0," _
& vbLf & "vote = false," _
& vbLf & "hasLicense = false," _
& vbLf & "})")

您可以在SO论坛上看到差异,因为它将文本格式设置为红色(查看您的代码与我的相比)

答案 2 :(得分:-1)

我猜你可能想要保存多个代码块。

如果您使用任何其他方法,您将手动使用HARD CODE每个代码块。

因此,开发一个子程序为你做驴工作是有道理的!

有些事情......

Function Stringify(ByVal Textdata As String) As String
  '
  Dim buf As String
  Dim counta As Integer
  '
  buf = ""
  For counta = 1 To Len(Textdata)
    If Mid(Textdata, counta, 1) = Chr(34) Then
      buf = buf & """ & chr(34) & """
    Else
      buf = buf & Mid(Textdata, counta, 1)
    End If
  Next
  Stringify = Chr(34) & buf & Chr(34)
  '
End Function

你可以把这个函数称为....

MyConvertedCodeString = Stringify(txtTextData.text)