在字符串的中间插入一个变量,但保留单引号(C#)

Nau*_*nja 2 c# string

如何将变量插入到一段字符串中,并将单引号作为字符串的一部分.

码:

var textMessage = "myText"; //local variable storing text to be injected to string
Run Code Online (Sandbox Code Playgroud)

这是我尝试过但它没有用的:

"//div[contains(@id,'msgError') and contains(text(),'+textMessage+')]" //string where textMessage should be insterted
Run Code Online (Sandbox Code Playgroud)

OUTPUT应该是这样的:

"//div[contains(@id,'msgError') and contains(text(),'myText')]" //Output
Run Code Online (Sandbox Code Playgroud)

Kam*_*ski 5

您可以像这样使用string.Format:

string.Format("//div[contains(@id,'msgError') and contains(text(),'{0}')]", textMessage)
Run Code Online (Sandbox Code Playgroud)

您可以指定要放入字符串的更多参数.