newDev
newDev

Reputation: 103

How to replace two different strings in iPhone programming

I am reading CSV file in my application.

I want to replace two different strings and print as one line. for example:

string1#$string2#$string3####string4

I want to replace #$ with , and #### with \n

and want to show the result on a UILabel.

Upvotes: 2

Views: 74

Answers (3)

P.J
P.J

Reputation: 6587

Try this

NSString *string = @"####abc#$de";
string = [string stringByReplacingOccurrencesOfString:@"####" withString:@"\n"];
string = [string stringByReplacingOccurrencesOfString:@"#$" withString:@","];

Hope it helps you

Upvotes: 0

Aman Aggarwal
Aman Aggarwal

Reputation: 3754

use

String = [String stringByReplacingOccurrencesOfString:@"#$" withString:@","];
String = [String stringByReplacingOccurrencesOfString:@"####" withString:@"\n"];

And then

yourLabel.text=String;

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726669

You can use stringByReplacingOccurrencesOfString:withString: method, like this:

NSString *orig = "string1#$string2#$string3####string4";
NSString *res = [orig stringByReplacingOccurrencesOfString:@"#$" withString:@" "];
res = [res stringByReplacingOccurrencesOfString:@"####" withString:@"\n"];

Note that the original string does not get changed: instead, a new instance is produced with the replacements that you requested.

Upvotes: 4

Related Questions