I wanted to know if theres a single method or way that will help me replace strings for specific characters.
like MALE - M
FEMALE - F
CHILD - P
The longer way out is this..
[str stringByreplacingOccurencesOfString:#"MALE" withString:#"M"];
[str stringByreplacingOccurencesOfString:#"FEMALE" withString:#"F"];
[str stringByreplacingOccurencesOfString:#"CHILD" withString:#"P"];
I was wondering if theres another way in which i can reduce lines of code here, specially when there are alots of things to replace.
thanks.
this is for iPhone OS.
No, but it shouldn't take more than 5-10 minutes to write a method that takes an array or varargs to do it for you.
Related
I have an algorithm whose intermediate step is to replace a substring with another substring. To be precise I have a string HBIN_NEW and I have another string P. I want to replace every 6th,7th,8th element of the string HREP with the 1st,2nd,3rd element of PBIN_NEW. For this i have written the code
For example If PBIN_NEW='1111111101010101' and HBIN_NEW='1111100010101010'
then the new string HREP
should be HREP='1111111110101101'
for k=1:8:262144*8
HREP=strrep(HBIN_NEW,HBIN_NEW(k+5:k+7),PBIN_NEW(k:k+2));
end
Is this code correct to implement the above idea. And if yes, it is taking a long time do this replacement scheme, can somebody suggest some optimized way of doing this.
The wording on the question is still a bit awkward, and I'm not exactly sure how to get the example HREP given the wording, but most likely strrep is overkill for what it sounds like you are trying to do. A simple loop with assignments would be fine:
HREP = HBIN_NEW;
for k=1:8:length(HBIN_NEW)
HREP(k+5:k+7) = PBIN_NEW(k:k+2);
end
Often times though it can be better to just enumerate the position assignments and avoid the loop. So instead you have something like this:
HREP = HBIN_NEW;
HREP(6:8:end) = PBIN_NEW(1:8:end);
HREP(7:8:end) = PBIN_NEW(2:8:end);
HREP(8:8:end) = PBIN_NEW(3:8:end);
I think that does what you want, or should get you close enough ...
Finally, a bit of unsolicited style advice. Although Matlab doesn't have a very strict code style guide, most likely the use of all caps with underscores is not the best way of naming your variables. I personally prefer lowercase with underscores, e.g. pbin_new and only use capitalized words for constants ...
I am new to Objective C and iPhone development.
I'm working with a basic calculator. I want to add a large value in text field. How can I display large values like this : 6.67543 x 10^ -34 in Xcode?
Thank you
You could set up a NSNumberFormatter with NSNumberFormatterScientificStyle and call stringFromNumber... However, the issue I think is going to be with the precision you are hoping to support requires a specialized framework. There might be one based on GNU MP Bignum library out there, but I don't know it off hand.
Use Double data type. And then convert it into string then pass that string to your textField.
For eg:-
double result;
NSString *strResult = [[NSNumber numberWithDouble:result] stringValue];
It will give you exponential string.
And instead of a UILabel use UITextField.
I am new to coding and objective C so thanks for the help in advance.
I have a .plist file containing an array of strings filled with formulas such as
*5.3
/2
-10.5
I am able to retrieve these string values from the .plist file but I am getting a little stuck trying to append these string formulas to existing variables with the hopes of returning a converted number. For example I would like to use my variable 7 with the formula *5.3 and return 37.1
7 *5.3 -> 37.1
Any help would be greatly appreciated
Appending the string to a variable is straightforward; it can be accomplished with something like this:
NSString *equation = [NSString stringWithFormat:#"%d%#", variable, plistEntry];
You'll run into problems when you want to evaluate this equation, however. This SO question discusses expression evaluation in Objective-C. Dave DeLong's answer links to a couple of libraries that you may want to look into: DDMathParser and GCMathParser.
This can't be done as-is. You'll need one of the many free expression evaluators (probably in C) that float around on the web.
See this SO question.
What is the most efficient way to copy an array of 1000 ints from one array to another in objective C?
This will be running on an iphone within some drawing code so its important to be as efficient as possible.
Thanks.
if the concern is efficiency, I'm assuming this is a C array of ints. If so, you can use memcpy().
For example:
int copyOfArr[4], arr[4] = {0,1,2,3};
memcpy(copyOfArr, arr, sizeof(arr));
Hope that helps.
If you are talking about a normal C array, then memcpy is the most efficient way. If you are talking about NSArray, then send a "copy" message.
I have been looking everywhere for an answer to this question - perhaps I'm looking in the wrong places. Also, I'm brand new to Objective C although I have around 10 years of experience as a developer.
for this code:
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
what would the method definition look like?
- (void)makeGroup:(Group *)g, (NSString *)memberOne, ...?
Thanks for any help you can provide. I know this is probably very simple...
Thanks,
R
It looks like you have a method that can take a variable number of arguments. If that's the case, the definition would look something like:
- (void)makeGroup:(Group *)g, ...;
Check out NSString's stringWithFormat: or NSArray's arrayWithObjects: methods for examples.
Edit: Upon further documentation reading, it seems that you are looking at the exact example that's in the Objective-C 2.0 documentation. The declaration you're looking for is right at the bottom of page 36.
You can receive an infinte number of arguments with an ellipsis (...). Check this for further details!
It would make more sense to have the members as a separate array argument, like -(void)makeGroup:(Group *)g members:(NSArray *)members. If you must do varargs (which is a pain), it should be written like -(void)makeGroup:(Group *)g members:(NSString *)firstMember, ....
Since I this is trying to figure out how an example method from the documentation would be declared, it would be like this:
- (void)makeGroup:(id)group, ...
Then you would start up the varags machinery with the group argument and use it to find the other arguments.
either you're looking for MrHen's answer if you're seeking to do your own class method or if you want to do them separately you write the following into your header file:
-(void)makeGroup:(Group *)g;
-(NSString *)memberOne;
EDIT: I answered the wrong question. Ignore this.
The correct way to do this is:
-(void)makeGroup:(Group *)g memberOne:(NSString *)memberOne memberTwo:(NSString *)memberTwo memberThree:(NSString *)memberThree {
...
}
The call will look like this:
[receiver makeGroup:group memberOne:memberOne memberTwo:memberTwo memberThree:memberThree];