我们如何快速删除除数字、点和冒号以外的所有字符? [英] How can we remove every characters other than numbers, dot and colon in swift?

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

问题描述

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

<title>Uaeexchange 移动应用程序</title></head><body><div id='ourMessage'>49.40:51.41:50.41</div></body></html>

我想获取包含 49.40:51.41:50.41 的字符串.我不想通过字符串提前或索引来做到这一点.我可以通过在swift中指定我只需要数字、点(.)和冒号(:)来获取这个字符串吗?我是说一些数字和一些特殊字符?

我试过了

let stringArray = response.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)让 newString = stringArray.joinWithSeparator("")打印(修剪\(newString)和计数\(newString.characters.count)")

但这显然也会去掉点和冒号.朋友有什么建议吗?

解决方案

对您问题的简单回答是您需要包含."&":" 在您要保留的集合中.

let response: String = "Uaeexchange 移动应用程序

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"

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

怎么办

Uaeexchange 移动应用程序<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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆