The following code is mainly implemented: 1, the interception of "@@" before the string, 2, remove the string "# #", 3, remove the space in the string and line-wrapping.
Hope to learn from each other.
-----VIEWCONTROLLER.M content is as follows:------
#import "ViewController.h"
#import "HandleString.h"
@interface Viewcontroller ()
{
NSString *_str;
NSString *_tempstr;
UILabel *_label;
}
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
#pragma-----1. Intercept the contents of the front of the @@ 前面. Remove # 3. Remove spaces for line wrapping
_str = @ "Quiet night think ##\n bed before the moon light \n# #疑是 # #地上霜 \ n Jutou @@ 望 at the moon \" head down to think hometown ";
_tempstr = _str;
[Self createsubview];
}
-(void) createsubview{
_label = [[UILabel alloc]init];
CGRect temp = self.view.frame;
Temp.origin.x + = 20;
TEMP.ORIGIN.Y + = 80;
Temp.size.width-= 40;
Temp.size.height = 150;
_label.frame = temp;
_label.textalignment = Nstextalignmentcenter;
_label.linebreakmode = nslinebreakbywordwrapping;
_label.numberoflines = 0;
_label.text = _str;
_label.textcolor = [Uicolor Greencolor];
_label.backgroundcolor = [[Uicolor bluecolor]colorwithalphacomponent:0.3];
[Self.view Addsubview:_label];
for (Nsinteger i = 0; i<4; i++) {
UIButton *btn = [UIButton Buttonwithtype:uibuttontypesystem];
Btn.frame = CGRectMake (0, Cgrectgetmaxy (_label.frame) +50* (i+1), 150, 40);
CGRect a = Btn.frame;
a.origin.x = SELF.VIEW.CENTER.X-A.SIZE.WIDTH/2;
Btn.frame = A;
Btn.backgroundcolor = [Uicolor Cyancolor];
Nsarray *arr = @[@ "intercept the contents of @@ 前面", @ "Remove # #", @ "remove the space to wrap", @ "restore"];
[BTN Settitle:arr[i] forstate:uicontrolstatenormal];
Btn.tag = ten + I;//tag are 10,11,12,13 respectively
[Btn addtarget:self Action: @selector (click:) forcontrolevents:uicontroleventtouchupinside];
[Self.view ADDSUBVIEW:BTN];
}
}
-(void) Click: (UIButton *) button{
Nsinteger x = Button.tag;
Switch (x) {
Case 10:
_TEMPSTR = [handlestring handlestring:_tempstr interceptfrom:nil to:@ "@"];
Break
Case 11:
_TEMPSTR = [handlestring handlestring:_tempstr replace:@ "# #" with:@ ""];
Break
Case 12:
_TEMPSTR = [handlestring delspaceandnewline:_tempstr];
Break
Case 13:
_tempstr = _str;
Break
Default
Break
}
_label.text = _tempstr;
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end
The contents of the------HandleString.h are as follows:---------
#import <Foundation/Foundation.h>
@interface Handlestring:nsobject
Replace str1 with STR2
+ (NSString *) handlestring: (NSString *) string Replace: (NSString *) str1 with: (NSString *) str2;
From STR1 (including), intercept string to STR2 (not included)
+ (NSString *) handlestring: (NSString *) string Interceptfrom: (NSString *) str1 to: (NSString *) str2;
Remove spaces from a string, line breaks
+ (NSString *) Delspaceandnewline: (NSString *) string;
@end
The contents of the------handlestring.m are as follows:---------
#import "HandleString.h"
@implementation handlestring
+ (NSString *) Delspaceandnewline: (NSString *) string; {
nsmutablestring *mutstr = [nsmutablestring stringwithstring:string];
Nsrange range = {0,mutstr.length};
[Mutstr replaceoccurrencesofstring:@ "" withstring:@ "" Options:nsliteralsearch Range:range];
Nsrange range2 = {0,mutstr.length};
[Mutstr replaceoccurrencesofstring:@ "\ n" withstring:@ "" Options:nsliteralsearch Range:range2];
return mutstr;
string = [string Stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]; Remove end-of-line whitespace characters and newline characters
string = [string stringbyreplacingoccurrencesofstring:@ "" withstring:@ ""];
string = [string stringbyreplacingoccurrencesofstring:@ "\ n" withstring:@ ""];
return string;
}
#pragma-----If there is str1 in the string, replace it with STR2
+ (NSString *) handlestring: (NSString *) string Replace: (NSString *) str1 with: (NSString *) str2; {
if (str2 = = nil) {
STR2 = @ "";
}
Way One
nsmutablestring *tempstr = [nsmutablestring stringwithstring:string];
Nsrange range = {0,tempstr.length};
[TempStr replaceoccurrencesofstring:str1 withstring:str2 options:nsliteralsearch Range:range];
return tempstr;
Way Two
string = [string Stringbyreplacingoccurrencesofstring:str1 withstring:str2];
return string;
Mode three
Nsarray *array = [string componentsseparatedbystring:str1];
Nsinteger count = [Array count]-1;
nsmutablestring *tempstr = [nsmutablestring stringwithstring:string];
for (Nsinteger i = 0; i<count; i++) {
Nsrange range = [TempStr rangeofstring:str1];
Nsinteger location = range.location;
Nsinteger length = range.length;
if (location! = Nsnotfound) {
[TempStr replacecharactersinrange:nsmakerange (location, length) WITHSTRING:STR2];
}
}
return tempstr;
}
#pragma------from the string str1, intercept to STR2
+ (NSString *) handlestring: (NSString *) string Interceptfrom: (NSString *) str1 to: (NSString *) str2; {
if (str1 = = nil) {
STR1 = @ "";
}
if (str2 = = nil) {
STR2 = @ "";
}
Nsrange Range1 = [string rangeofstring:str1];
Nsinteger location1 = range1.location;
if (location1! = nsnotfound) {
string = [string Substringfromindex:location1];
}
Nsrange range2 = [string rangeofstring:str2];
Nsinteger location2 = range2.location;
if (location2! = nsnotfound) {
string = [string Substringtoindex:location2];
}
return string;
}
@end
END
iOS String processing _ Replace (remove space wrapping), intercept