How can I remove quotes from an NSString? - iphone

I am trying to remove quotes from something like:
"Hello"
so that the string is just:
Hello

Check out Apple's docs:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/
You probably want:
stringByReplacingOccurrencesOfString:withString:
Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
So, something like this should work:
newString = [myString stringByReplacingOccurrencesOfString:#"\"" withString:#""];

I only wanted to remove the first quote and the last quote, not the quotes within the string so here's what I did:
challengeKey = #"\"I want to \"remove\" the quotes.\"";
challengeKey = [challengeKey substringFromIndex:1];
challengeKey = [challengeKey substringToIndex:[challengeKey length] - 1];
Hope this helps others looking for the same thing. NSLog and you'll get this output:
I want to "remove" the quotes.

Related

Ignore case UITextField/UITextView

I would like to ignore all case sensitivity to be ignored for my UITextField and UITextView. I would also like to know how to detect if the string is upperCase or lowerCase. Thanks.
[myString uppercaseString] will give you the myString variable transformed to use only uppercase letters, [myString lowercaseString] will give you the lowercase version, respectively.
You can use it to check for uppercase (lowercase) strings like this:
if ([[textField text] isEqualToString:[[textField text] uppercaseString]]) {
NSLog("String is uppercase!");
}
If you have a reference string and want to compare it ignoring the case, you can just use caseInsensitiveCompare: method of NSString:
[referenceString caseInsensitiveCompare:[textField text]];

NSString question - rangeOfString method

I am having an issue that I can't figure out.
I'm trying to run the rangeOfString method on a string, and I'm not sure how to determine if the string was not found. For example:
NSRange range = [#"abc" rangeOfString:#"d" options:NSCaseInsensitiveSearch range:NSMakeRange(0,3)];
Clearly, "d" is not contained in the string "abc." I'd like to be able to do this:
if(the range is empty since "d" is not in "abc")
//do something
What is the code for this?
Thanks!!
From the documentation of NSString
-[NSString rangeOfString]
Return Value
An NSRange structure giving the
location and length in the receiver of
the first occurrence of aString.
Returns {NSNotFound, 0} if aString is
not found or is empty (#"").
So it looks like:
if ([#"abc" rangeOfString:#"d"].location == NSNotFound){
//Do something
Is the Apple-approved way.
EDIT:
I made a really bad typo, fixed it, thanks Kalle.
Check the length of the range. If it's non-zero, it was found.

How to get the string value from a string which contains ","?

I have a string value where it contains comma. Ex:- 1,234. I want to get the value of the string where i need only 1234. Can you please help me...
Instead of manually stripping out the commas, it might be more elegant (and less error-prone if you support different locales) to use an NSNumberFormatter to convert the string to a number.
NSString *myString = "1,234";
NSString *resultString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
If you want to strip the comma then: -
NSString *string = #"1,234";
string = [string stringByReplacingOccurrencesOfString:#"," withString:#""];
This should return you a string with just 1234 in it.
By 'getting the value' do you mean, converting this to a NSNumber object? If so use this
NSNumber *numberFromString = [NSNumber numberWithInteger:[string integerValue]];
I don't know that framework/language, but if an integer converter won't work, then strip out the commas by replacing them with null from the string and then convert to an integer.

Add backslash to String in Objective-c

I have a problem identical to this problem here.
I even want to encode the same infromation as him (it's a date/time for asp.net)...
When ever I try to add a backslash i get two backslashes since I used \.
Everyone in the thread above has claimed that this is a problem with NSLog and that NSString does treat \\ as a \. I have checked this further by using a packet sniffer to examine the packets I'm sending to the webserver and I can confirm that it is transmitting a double backslash instead of a single backslash.
Does anyone know how to add a backslash to a NSString?
The strings and NSLog are working fine for me:
NSLog(#"\\"); // output is one backslash
NSLog(#"\\\\"); // output is two backslashes
NSLog(#"\\/Date(100034234)\\/"); // output is \/Date(100034234)\/
What am I missing?
Try this:
yourStr = [yourStr stringByReplacingOccurrencesOfString:#"\\\\" withString:#"\\"];
NSLog(#"%#", yourStr);
I had the same problem, turned out that my JSON Parser replaced all occurrances of "\\" with "\\\\", so when I NSLogged my original code like this:
NSString *jsonString = [myJSONStuff JSONRepresentation];
NSLog(#"%#", jsonString);
This is what I got:
{TimeStamp : "\\/Date(12345678)\\/"}
However, the string itself contained FOUR backslashes (but only 2 of them are printed by NSLog).
This is what helped me:
NSString *jsonString = [myJSONStuff JSONRepresentation];
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"\\\\" withString:#"\\"];
NSLog(#"%#", jsonString);
The result:
{TimeStamp : "\/Date(12345678)\/"}

RegExKitLite Expression Question

I'm having trouble coming up with an RegExKitLite expression that will match. I'm parsing a string and I want it to grab everything till it comes upon the first occurrence of a colon
What would be the expression in RegExKitLite to do that?
Thanks!
This regex will match everything from the start until (but excluding) the first colon:
^[^:]*
To include the first colon is as simple as putting it on the end:
^[^:]*:
So, to use either of those with RegexKitLite, you can do:
NSString * firstItem = [someString stringByMatching:#"^[^:]*" capture:0];
Note how there is no parentheses - since * is greedy you can simply use the negated class and use captured group 0 (i.e. the whole match).
It's worth noting that most languages will include functions that allow you to do this with a regular function, for example ListFirst(MyString,':') or MyString.split(':')[0]
I suspect Objective-C has something similar to this ... yep, see here
NSString *string = #"oop:ack:bork:greeble:ponies";
NSArray *chunks = [string componentsSeparatedByString: #":"];
To do this specifically with RegexKitLite, you'll need to do the following:
Add the RegexKitLite.h/.m files to your project
Import RegexKitLite.h into the file where you need to use regular expressions
Use the following to grab the stuff before the colon:
NSString * everythingBeforeTheColon = [someString stringByMatching:#"([^:]*):" capture:1];
I just updated my SO answer here, so I figured I'd use that to benchmark the standard foundation componentsSeparatedByString: and RegexKitLites componentsSeparatedByRegex:. The line of code inside the for() loop for each was (essentially):
NSString *string = #"oop:ack:bork:greeble:ponies";
for() { NSArray *chunks = [string componentsSeparatedByString: #":"]; }
for() { NSArray *chunks = [string componentsSeparatedByRegex: #":"]; }
Times returned were (time is in microseconds per operation):
componentsSeparatedByString: 3.96810us
componentsSeparatedByRegex: 2.46155us
EDIT:
I thought I'd go one better: How to use RegexKitLite to create a NSArray of NSArrays from a string containing multiple lines of colon separated data (ie, /etc/passwd). Modified from the comma separated value example in the RegexKitLite documentation. When finished, the variable splitLinesArray contains the finished product.
NSString *theString = #"a:b:c\n1:2:3\nX:Y:Z\n"; // An example string to work on.
NSArray *linesArray = [theString componentsSeparatedByRegex:#"(?:\r\n|[\n\v\f\r\\x85\\p{Zl}\\p{Zp}])"];
id splitLines[[linesArray count]];
NSUInteger splitLinesIndex = 0UL;
for(NSString *lineString in linesArray) { splitLines[splitLinesIndex++] = [lineString componentsSeparatedByRegex:#":"]; }
NSArray *splitLinesArray = [NSArray arrayWithObjects:splitLines count:splitLinesIndex];