ios - NSString URL decode? [duplicate] - ios

This question already has answers here:
urldecode in objective-c
(6 answers)
Closed 9 years ago.
I'm trying to Decode URL,but i'm getting warning message like "NSString may not respond to URLDecode".
NSString* token = [[urlStr substringFromIndex:r.location + 1] URLDecode];
Any ideas?

Because it is not a method a classic NSString respond to, you can though add a category like the following, to add the method yourself :
#interface NSString (URLDecode)
- (NSString *)URLDecode;
#end
#implementation NSString
- (NSString *)URLDecode
{
NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:#"+" withString:#" "];
result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return result;
}
#end

Try like this.
NSString* urlEncoded = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
and check this link https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/TP40003744
it may help you.

Related

Appending NSString with Zeros depending upon length [duplicate]

This question already has answers here:
objective-c right padding
(3 answers)
Closed 5 years ago.
I have a string, length may vary but I have to check if its length is less then 12 and if yes append those many zeros to it.
Like:
str = Test123, output should be Test12300000
str = Test123456, output should be Test12345600
Only 12 Char is allowed in string.
Tried below code but not getting generic result. There must be a better and easy way to do.
- (NSString *)formatValue:(NSString *)str forDigits:(NSString *)zeros {
return [NSString stringWithFormat:#"%#%#", str ,zeros];
}
how about something like this:
- (NSString *)stringToFormat:(NSString *)str {
while (str.length <12) {
str = [NSString stringWithFormat:#"%#0", str];
}
return str;
}
This is fast and simple, I think.
- (NSString *)stringToFormat:(NSString *)str {
return [str stringByAppendingString:[#"000000000000" substringFromIndex:str.length]];
}
Just make sure the string is never more than 12 characters.

Cant get return data with cURL in OBJ-C

so im trying to make commands for my iOS tweak! So I think I would be using cURL! and it would be with the GET method.
As an example, if i wrote the command "$ud test" and clicked enter it would return the data from website (https://k8.reko.io/v1/api.php?ud=test) and replace the "$ud test" with the data after i hit send/enter. I have something written out, but I know its wrong. I cant seem to find any tutorials that I can understand online! I should mention that I am a beginner! Thank you!
%hook CoreDataConversationManager
- (void) sendTextMessage:(id)command withRenderInstructionSet:(id)arg1 toConversation:(id)arg2 isSuggestedResponse:(BOOL)arg3
{
NSString *url = #"https://K8.reko.io/v1/api.php?ud=";
NSMutableString *userInput = NSMutableString.alloc.init;
[url appendString:(#"%#", userInput )];
if ([command hasPrefix:#"$ud"]) command = userInput;
return %orig;
}
%end
Instead of this:
NSString *url = #"https://K8.reko.io/v1/api.php?ud=";
NSMutableString *userInput = NSMutableString.alloc.init;
[url appendString:(#"%#", userInput )];
you want is something like:
NSString *userInput = #"userInput";
NSString *urlStringPartial = #"https://K8.reko.io/v1/api.php?ud=";
NSString *urlStringFull = [urlStringPartial stringByAppendingPathComponent:userInput];
NSLog(#"urlStringFull: %#", urlStringFull);
urlStringFull: https:/K8.reko.io/v1/api.php?ud=/userInput

How to remove characters from an NSString and replace with spaces? [duplicate]

This question already has answers here:
String replacement in Objective-C
(6 answers)
Closed 8 years ago.
I have a NSString:
NSString *string1 = #"http://example.com/this-is-an-example-post"
By removing a range of characteres I have substringed it to:
NSString *string2 = #"this-is-an-example-post"
But I am struggling to get it into the form of:
NSString *string3 = #"this is an example post"
Any help? Much appreciated.
For example this way
string3 = [string2 stringByReplacingOccurrencesOfString:#"-" withString:#" "];

How to convert NSString to NSMutableString in Objective-C? [duplicate]

This question already has answers here:
How to initialize NSString to NSMutableString?
(4 answers)
Closed 8 years ago.
Is there any way to convert an NSString to NSMutableString in Objective-C?
NSString *string = #"A string.";
NSMutableString *mutableString = [string mutableCopy];

Extracting from the right of a string in objective C [duplicate]

This question already has answers here:
Get last 2 characters of a string?
(2 answers)
Closed 9 years ago.
This seems to be what I'm looking for but in reverse. I would like the string to extract from the right not from the left.
The example extracting from the left is given:
NSString *source = #"0123456789";
NSString *firstFour = [source substringToIndex:4];
Output: "0123"
I'm looking for a version of the below that works from the right (what is below doesn't work)
NSString *source = #"0123456789";
NSString *lastFour = [source substringToIndex:-4];
Output: "6789"
the [source substringFromIndex:6]; won't work because sometimes I will get an answer that is 000123456789 or 456789 or 6789. In all cases I just need the last 4 characters from the string so that I can convert it to a number.
there must be a better way than a bunch of if else statements?
As you are not sure, about the length of the string, so you must check it before extracting like this:
NSString *source = #"0123456789";
NSNumber *number;
if (source.length>=4) {
NSString *lastFour=[source substringFromIndex:source.length-4];
number=#([lastFour integerValue]); //and save it in a number, it can be int or NSInteger as per your need
}
NSLog(#"%#",number);
Also if you want a quick method that you need to call several times, create a category :
#implementation NSString (SubstringFromRight)
-(NSString *)substringFromRight:(NSUInteger)from{
if (self.length<from) {
return nil;
}
return [self substringFromIndex:self.length-from];
}
#end
And use it as :NSLog(#"%#",[source1 substringFromRight:4]);
NSString *source = #"0123456789";
NSString *newString = [source substringFromIndex:[source length] - 4];
NSLog(#"%#",newString);
replace
NSString *lastFour = [source substringToIndex:-4];
with
NSString *lastFour = [source substringFromIndex:[source length] - 4];
which returns you the last 4 characters of your original string string in lastFour string.
You can use the following code to get last 4 characters from your string.
NSString *last4Characters = [source substringFromIndex:(source.length - 4)];
NSLog(#"Last 4 Characters:%#",last4Characters);
last4Characters=nil;
Please let me know if any issue.

Resources