如何从NSString中删除十六进制字符 [英] How to remove hexadecimal characters from NSString

查看:151
本文介绍了如何从NSString中删除十六进制字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个问题,它涉及字符串中的一些hexa值,我需要从NSString中删除十六进制字符。

问题是当我打印的对象打印为BLANK行时。在调试模式下,它显示如下:



早先的问题:
如何检测ios中的垃圾字符串值?

解决方案

由于您的数据集显然具有垃圾值,您可以使用此方法检查您的字符串是否有效。定义您的验证标准,并且不要招待垃圾的价值。但正如gnasher之前所建议的那样,您应该找出导致垃圾数据插入数据库的错误。完成之后,检查输入的字符串是否符合您定义的条件。如果有,就做你想做的。

   - (BOOL)isValidString:(NSString *)input 
{
NSMutableCharacterSet * validSpecialChars = [NSMutableCharacterSet characterSetWithCharactersInString:@_〜。,]; //在此添加所需字符
[validSpecialChars formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
return [[input stringByTrimmingCharactersInSet:validSpecialChars] isEqualToString:@];



$ b $ p
$ b

如果你的字符串只包含你定义的字符,它将返回true。如果它包含任何其他字符(垃圾或无效),它将返回false。


I am facing one issue related some hexa value in string, i need to remove hexadecimal characters from NSString.

The problem is when i print object it prints as "BLANK line". And in debug mode it shows like :

So how can i remove it from the string?

EDIT

Triming whitespace :

result of NSLog is :

2015-12-14 15:37:10.710 MyApp [2731:82236] tmp :''

Database:

Earlier question: how to detect garbage string value in ios?

解决方案

As your dataset clearly has garbage values, You can use this method to check if your string is valid or not. Define your validation criteria and simply don't entertain the values which are garbage. But as suggested before by gnasher, you should rather look for the bug which is causing insertion of garbage data in your database. Once you have done that, check if the input string matches your defined criteria. If it does, do what you want. If it doesn't, simply move on.

-(BOOL) isValidString: (NSString*) input
{
         NSMutableCharacterSet *validSpecialChars = [NSMutableCharacterSet characterSetWithCharactersInString:@"_~.,"];//Add your desired characters here
         [validSpecialChars formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
         return [[input stringByTrimmingCharactersInSet:validSpecialChars] isEqualToString:@""];
}

If your string will contain only your defined characters, it will return true. If it contains any other characters (garbage or invalid) it will return false.

这篇关于如何从NSString中删除十六进制字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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