Replacing a single occurrence of string Obj-c - objective-c

I am trying to replace only one of the occurrences of a NSString with another string. I know that you can use stringByReplacing*Occurrences*OfString; however, that replaces all occurrences. What I am looking for is more like stringByReplacing*Occurrence*OfString. If anyone could help me that would be great. Thanks,
Jnani

Something like this?
NSRange location = [someString rangeOfString:stringToReplace];
NSString* result = [someString stringByReplacingCharactersInRange:location withString:stringToReplaceWith];

Related

Objective C - NSRange and rangeOfString

I have a little problem with NSRange and rangeOfString. I want to search a substring in a given string which is working fine, but only to find a exact string and theres the problem i need to find a substring which begins always the same and ends always the same. I tried it already with something like that:
match = [strIn rangeOfString: #"truni/begin/*/end"];
But thats not working. So i need a way to to do this. Here is the specific part of the Code in full:
NSRange match;
match = [strIn rangeOfString: #"turni/begin/sHjeUUej/end"];
NSRange range = NSMakeRange(match.location, match.length);
NSString *strOut = [strIN substringWithRange:range];
You see the string "turni/begin/sHjeUUej/end" will always be the same except for the part "sHjeUUej". Hope someone can help me.
Thanks in advance.
Use a regular expression with:
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask
with an option of RegularExpressionSearch.
See ICU User Guide Regular Expressions for information on creating regular expressions.
you can use prefix/suffix
if ([strIn hasPrefix:#"truni/begin/"] && [strIn hasSuffix:#"end"]) {
//match
You can use a simpler solution if you make sure that your string always starts with turni/begin/ and ends with /end.
You can use:
NSString *strOut = [[strIn stringByReplacingOccurrencesOfString:#"turni/begin/" withString:#""] stringByReplacingOccurrencesOfString:#"/end" withString:#""];
With that, you can retrieve the string between the two others with only one line of code and less comparations.

parse strings separated by 2 elements

On Mac OS X, in Ojective-C, I wanted to know if anyone could give me some pointers as to how I could parse strings contained between "x" and "y".
My current code only enables me to separate the strings separated by one componennt :
NSArray * allLines = [theDataObject componentsSeparatedByString:#"word-1"];
Ideally, I would like to isolate the strings contained between #"word-1" and #"word-2".
Could anyone help please? Thanks in advance!
Check the documentation for NSScanner. The methods you want are -scanUpToString:intoString: and -scanString:intoString:
NSString *str = [theDataObject stringByReplacingOccurencesOfString:#"word-2" withString:#"word-1"];
NSArray *allLines = [str componetnsSeperatedByString:#"word-1"];
You don't care whether its word-1 or word-2 as those do not come back in the array anyways.

to know the character in a object-c nsstring object

I have an NSString *str and I want to know if the 2nd character is equal to a. How can I do this? I'm new to objective c.
Use this:
[str characterAtIndex:1]
But I always encourage you to check documentation first!
You could also use:
[[str substringWithRange:NSMakeRange(1,1)] isEqualToString:#"a"];
Admittedly, this only becomes handy when you have longer strings for which you're searching.

How to make a "0000001" NSString

Another noob question from me.
I'm trying to create an application that counts, and I'd like to display the number value as something like "000001". So when it counts up it'll display as "000002".
I'm using something like this right now, but it's not working:
counter.text = [NSString stringWithFormat:#"%000f", count];
The counter.text is a UILabel.
Thanks for the answer.
If you're storing the number as an integer, use the %0xd format (where x is the number of digits) like so:
counter.text = [NSString stringWithFormat:#"%06d", count];
try this
NSLog(#"%.6d",3);
more info please refer to NSDataformatter.

NSString stringWithFormat problem

I have such a problem:
double calcValue;
NSString *buf = [NSString stringWithFormat:#"%14f", myCalculator.calcValue];
buf = "7.142857";
istead of
buf = "7.1428571428571423";
I tried to format it in such ways: #"%f".
But when I tried #"%14.9f" 9 signs after comma displayed.
Whats the problem? And how can I solve it? Thanx!
Unless I'm misunderstanding, the correct format would be #"%.14f.
What you did, #"%14f" says you want up to 14 digits, before the decimal, but you didn't specify digits after.
I'm not sure why, but stringWithFormat: doesn't seem to format doubles properly. You might try this approach:
double calcValue=7.1428571428571423;
NSString *buf=[[NSNumber numberWithDouble:calcValue] stringValue];
which will set buf to #"7.142857142857142".
For tighter control over number of digits, you could use a NSNumberFormatter.