删除 Swift 5 中的转义"\"字符



我正在尝试使用 Alamofire 5 上传 xml 文件multipartFormData.对于数据部分,它必须String,当我将 xml 文件制作为字符串时,它包含转义字符。我尝试了replacingOccurrences(of: """, with: "")但它删除了 xml 中的所有字符串。如果我尝试replacingOccurrences(of: "", with: "")它会, with: "")变成字符串。有没有办法删除反斜杠?谢谢。

**请不要标记重复,因为我尝试了所有答案,但没有处理我的情况**

下面是示例 xml

<code displayName="Vital signs" codeSystemName="LOINC" code="8716-3" codeSystem="12345"></code>
<title>Vital Signs</title>
<text>Measurement period:null-null
<table>
<thead>
<tr>
<th>Measurement</th>
<th>Date/Time</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Mean artillery pressure</td>
<td>2019-10-15/12:11</td>
<td>8</td>
</tr>
</tbody>
</table>
</text>
<entry typeCode="DRIV">

以前

在我尝试xml.replacingOccurrences(of: """, with: "")之后 它删除 XML 中的 ">

从评论:

@CRD我在调试控制台中做了po并复制并粘贴到一个空文件中。正如您在图像中看到的,那里有\"。我尝试在空文件中手动删除它,并使用邮递员上传到它工作的服务器。但我不能在 Xcode 中做到这一点。

看起来您的字符串中没有任何反斜杠。简单代码:

func applicationDidFinishLaunching(_ aNotification: Notification)
{
let withQuotes = "A "quoted" word"
print("withQuotes |(withQuotes)|")
}

将断点放在最后一个支撑上并运行:

withQuotes |A "quoted" word|
(lldb) po withQuotes
"A "quoted" word"

"po"将字符串的值显示为文本 - 引号和转义,但字符串不包括这些引号或转义。

对于要删除字符串值中的反斜杠的记录,您可以使用:

.replacingOccurrences(of: "\", with: "")

在字符串文本或变量上调用。

呵呵

最新更新