0

I have a long string. I'd like to take this long string, search for any occurrences of words that appear between quotes (i.e., "string"), and insert a string before the word (i.e., "x"), and a string after the word (i.e., "y").

Any solutions would be most appreciated! Thanks!

I see that I could use the following to grab the text between the quotes:

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([\"])    
(?:\\\\\\1|.)*?\\1" options:0 error:&error];

NSRange range = [regex rangeOfFirstMatchInString:myString options:0 range:NSRangeMake(0,     
[myString length]];

However, now I need to replace the text that is inside the quotations, inserting the html tags "bold" before and "/bold" after. Is there anyway for me to do this? Also, if there are multiple occurrences of quoted text in a given string, how would I use the above code to cycle through the string to get modify each piece of quoted text one-by-one?

I came across this post ([click here]]1 but I'm not quite sure how to modify the sample code to achieve the result I want. Any help would be great!

Community
  • 1
  • 1
ehul
  • 19
  • 1
  • 6
  • So the string will have escaped quotes? – brthornbury Aug 29 '12 at 21:29
  • I will refer you to this page: http://stackoverflow.com/questions/510269/how-do-i-concatenate-strings-in-objective-c T The page talks about adding strings together by using two NSMutable strings and adding one to both, which seems to be the preferable of the two it gives. Unfortunately, there is **no** operation to add two or more strings together (which really sucks). – Justin A. Aug 30 '12 at 01:49
  • @ALL: What I would essentially like to do is find quoted text within my string (i.e., "The quick 'brown fox' jumped over the...") and insert "" and "" inside the quoted text. The end result would be: "The quick 'brown fox' jumped over the...." It's HTML code and I would like definitions in the HTML code to be bolded. Any ideas? – ehul Aug 30 '12 at 01:57

2 Answers2

1

I will refer you to this page: Shortcuts in Objective-C to concatenate NSStrings

The page talks about adding strings together by using two NSMutable strings and adding one to both, which seems to be the preferable of the two it gives. Unfortunately, there is no operation to add two or more strings together (which really sucks).

Community
  • 1
  • 1
Justin A.
  • 73
  • 6
0

Try this:

NSString *original=@"The quick 'brown fox' The quick 'brown fox' ";
NSString *target=[original stringByReplacingOccurrencesOfString:@"'brown fox'" withString:@"<b>brown fox</b>"];
Mil0R3
  • 3,876
  • 4
  • 33
  • 61
  • Thanks for the suggestion. Trouble is, I do not know exactly what the text is that is appearing between the quotes. It could be anything, so I cannot do a "stringByReplacingOccurencesOfString". – ehul Aug 30 '12 at 13:51