NSString+extension.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /********************************************************************************************************
  2. * @file NSString+extension.m
  3. *
  4. * @brief for TLSR chips
  5. *
  6. * @author telink
  7. * @date Sep. 30, 2010
  8. *
  9. * @par Copyright (c) 2010, Telink Semiconductor (Shanghai) Co., Ltd.
  10. * All rights reserved.
  11. *
  12. * The information contained herein is confidential and proprietary property of Telink
  13. * Semiconductor (Shanghai) Co., Ltd. and is available under the terms
  14. * of Commercial License Agreement between Telink Semiconductor (Shanghai)
  15. * Co., Ltd. and the licensee in separate contract or the terms described here-in.
  16. * This heading MUST NOT be removed from this file.
  17. *
  18. * Licensees are granted free, non-transferable use of the information in this
  19. * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
  20. *
  21. *******************************************************************************************************/
  22. //
  23. // NSString+extension.m
  24. // SigMeshOCDemo
  25. //
  26. // Created by 梁家誌 on 2018/8/2.
  27. // Copyright © 2018年 Telink. All rights reserved.
  28. //
  29. #import "NSString+extension.h"
  30. @implementation NSString (extension)
  31. /*
  32. *去掉首尾空格
  33. */
  34. - (NSString *)removeHeadAndTailSpace{
  35. return [self stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
  36. }
  37. /*
  38. *去掉首尾空格 包括后面的换行 \n
  39. */
  40. - (NSString *)removeHeadAndTailSpacePro{
  41. return [self stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
  42. }
  43. /*
  44. *去掉所有空格
  45. */
  46. - (NSString *)removeAllSapce{
  47. return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
  48. }
  49. /*
  50. *去掉所有空格和最后的回车
  51. */
  52. - (NSString *)removeAllSapceAndNewlines{
  53. NSString *tem = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  54. return [tem stringByReplacingOccurrencesOfString:@" " withString:@""];
  55. }
  56. /// 去掉所有空格和最后的回车,并在字符串前面补充“0”使其满足长度length
  57. /// @param length 需要返回的字符串的长度,大于该长度直接返回,不在补“0”
  58. - (NSString *)formatToLength:(UInt8)length {
  59. NSString *tem = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  60. tem = [tem stringByReplacingOccurrencesOfString:@" " withString:@""];
  61. while (tem.length < length) {
  62. tem = [@"0" stringByAppendingString:tem];
  63. }
  64. return tem;
  65. }
  66. /*
  67. *循环在间隔n个字符添加m个空格
  68. */
  69. - (NSString *)insertSpaceNum:(int)spaceNum charNum:(int)charNum{
  70. if (charNum <= 0) {
  71. return @"";
  72. }
  73. NSString *returnString = @"";
  74. for (int i=0; i<self.length; i++) {
  75. returnString = [returnString stringByAppendingString:[self substringWithRange:NSMakeRange(i, 1)]];
  76. if ((i + 1) % charNum == 0) {
  77. for (int j=0; j<spaceNum; j++) {
  78. returnString = [returnString stringByAppendingString:@" "];
  79. }
  80. }
  81. }
  82. return returnString;
  83. }
  84. ///JSON字符串转化为字典
  85. + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
  86. {
  87. if (jsonString == nil) {
  88. return nil;
  89. }
  90. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  91. NSError *err;
  92. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
  93. options:NSJSONReadingMutableContainers
  94. error:&err];
  95. if(err)
  96. {
  97. DDLog(@"json解析失败:%@",err);
  98. return nil;
  99. }
  100. return dic;
  101. }
  102. @end