本文介绍了我们如何快速删除除数字、点和冒号以外的所有字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直无法从 html body 获取字符串

49.40:51.41:50.41</div></body></html>"var s: CharacterSet = CharacterSet.decimalDigitss.insert(charactersIn: ".:")让 stringArray: [String] = response.components(separatedBy: s.inverted)让 newString: String = stringArray.joined(separator: "")打印(修剪'\(新字符串)'和计数=\(newString.characters.count)")//"修剪 '49.40:51.41:50.41' 和 count=17\n"

如果没有关于您的其他回复可能是什么的更多信息,我真的无法给出更好的答案,但从根本上这不是一个好的解决方案.如果响应是

怎么办

<div id='2'>其他一些东西:像这样</div><div id='ourMessage'>49.40:51.41:50.41</div></body></html>

对此使用替换/删除解决方案是一种技巧,而不是一种算法——它会一直工作,直到它不工作为止.我认为您可能应该寻找 <div id='ourMessage'> 并从那里读取到下一个 <,但同样,我们需要有关响应格式规范的更多信息.

I am stuck at getting a string from html body

<html><head>
<title>Uaeexchange Mobile Application</title></head><body>
<div id='ourMessage'>
    49.40:51.41:50.41
</div></body></html>

I Would like to get the string containing 49.40:51.41:50.41 . I don't want to do it by string advance or index. Can I get this string by specifying I need only numbers,dot(.) and colon(:) in swift. I mean some numbers and some special characters?

I tried

let stringArray = response.componentsSeparatedByCharactersInSet(
                    NSCharacterSet.decimalDigitCharacterSet().invertedSet)
                let newString = stringArray.joinWithSeparator("")
                print("Trimmed\(newString)and count\(newString.characters.count)")

but this obviously trims away dot and colon too. any suggestions friends?

解决方案

The simple answer to your question is that you need to include "." & ":" in the set that you want to keep.

let response: String = "<html><head><title>Uaeexchange Mobile Application</title></head><body><div id='ourMessage'>49.40:51.41:50.41</div></body></html>"

var s: CharacterSet = CharacterSet.decimalDigits

s.insert(charactersIn: ".:")

let stringArray: [String] = response.components(separatedBy: s.inverted)

let newString: String = stringArray.joined(separator: "")

print("Trimmed '\(newString)' and count=\(newString.characters.count)")
// "Trimmed '49.40:51.41:50.41' and count=17\n"

Without more information on what else your response might be, I can't really give a better answer, but fundamentally this is not a good solution. What if the response had been

<html><head><title>Uaeexchange Mobile Application</title></head><body>
     <div id='2'>Some other stuff: like this</div>
     <div id='ourMessage'>49.40:51.41:50.41</div>
</body></html>

Using a replace/remove solution to this is a hack, not an algorithm - it will work until it doesn't.I think you should probably be looking for the <div id='ourMessage'> and reading from there to the next <, but again, we'd need more information on the specification of the format of the response.

这篇关于我们如何快速删除除数字、点和冒号以外的所有字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:20