IOS string processor _ Replace (remove space line breaks), intercept, and ios line breaks

Source: Internet
Author: User

IOS string processor _ Replace (remove space line breaks), intercept, and ios line breaks

The following code mainly implements: 1. Extract the string before "@"; 2. Remove "#" from the string; 3. Remove spaces and line breaks from the string.

Hope to learn from each other and correct each other.

 

----- ViewController. m content :------

 

# 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 content above @ 2. Remove #3. Remove the space and line feed

_ Str = @ "# ## \ n moonlight in front of bed \ n # suspect # ground frost \ n raise your head @ \ n bow to your 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. size = 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 =;

Btn. backgroundColor = [UIColor cyanColor];

NSArray * arr = @ [@ "intercept @ content above", @ "Remove ##", @ "remove space and line feed", @ "Restore"];

[Btn setTitle: arr [I] forState: UIControlStateNormal];

Btn. tag = 10 + I; // The tag values are 10, 11, 12, and 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 that can be recreated.

}

 

@ End

 

 

------ HandleString. h :---------

# Import <Foundation/Foundation. h>

 

@ Interface HandleString: NSObject

 

// Replace str1 with str2

+ (NSString *) handleString :( NSString *) string replace :( NSString *) str1 with :( NSString *) str2;

 

// Extract the string from str1 (included) to str2 (not included)

+ (NSString *) handleString :( NSString *) string interceptFrom :( NSString *) str1 to :( NSString *) str2;

 

// Remove spaces and line breaks from the string

+ (NSString *) delSpaceAndNewline :( NSString *) string;

 

@ End

------ HandleString. m :---------

# Import "HandleString. h"

 

@ Implementation HandleString

 

+ (NSString *) delSpaceAndNewline :( NSString *) string ;{

 

NSMutableString * mutStr = [NSMutableString stringWithString: string];

Nsange range = {0, mutStr. length };

[MutStr replaceOccurrencesOfString: @ "" withString: @ "" options: NSLiteralSearch range: range];

Nsange range2 = {0, mutStr. length };

[MutStr replaceOccurrencesOfString: @ "\ n" withString: @ "" options: NSLiteralSearch range: range2];

Return mutStr;

// String = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]; // remove the leading and trailing spaces and line breaks

// String = [string stringByReplacingOccurrencesOfString: @ "" withString: @ ""];

// String = [string stringByReplacingOccurrencesOfString: @ "\ n" withString: @ ""];

// Return string;

}

 

# Pragma ----- replace str1 with str2 if the string contains str1.

+ (NSString *) handleString :( NSString *) string replace :( NSString *) str1 with :( NSString *) str2 ;{

If (str2 = nil ){

Str2 = @"";

}

/// Method 1

// NSMutableString * tempStr = [NSMutableString stringWithString: string];

// Nsange range = {0, tempStr. length };

// [TempStr replaceOccurrencesOfString: str1 withString: str2 options: NSLiteralSearch range: range];

// Return tempStr;

/// Method 2

// String = [string stringByReplacingOccurrencesOfString: str1 withString: str2];

// Return string;

// Method 3

NSArray * array = [string componentsSeparatedByString: str1];

NSInteger count = [array count]-1;

NSMutableString * tempStr = [NSMutableString stringWithString: string];

For (NSInteger I = 0; I <count; I ++ ){

Nsange 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 ------ truncate str1 from string to str2

+ (NSString *) handleString :( NSString *) string interceptFrom :( NSString *) str1 to :( NSString *) str2 ;{

If (str1 = nil ){

Str1 = @"";

}

If (str2 = nil ){

Str2 = @"";

}

Nsange range1 = [string rangeOfString: str1];

NSInteger location1 = range1.location;

If (location1! = NSNotFound ){

String = [string substringFromIndex: location1];

}

Nsange range2 = [string rangeOfString: str2];

NSInteger location2 = range2.location;

If (location2! = NSNotFound ){

String = [string substringToIndex: location2];

}

Return string;

}

 

 

@ End

 

END

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.