I want to go through what the user input. The user is going to input some 'useless' information that i don't need right now. How would i get the slice the string so I can get the part I want.
for example:
NSString *findingText = [prefs stringForKey:userSearching];
NSString *substring = [string substringFromIndex:[string length] + 4];
But this is where i have put a specific character that i would like to search for which is !. The user has entered some information that I have used as the key. So when the user wants to find the info all they have to do is search for it and will find the special character and use that to get the NSUserDefaults. but what the User Enters doesn't always have the same length. so was wondering how I could do this.
Thanks in advance.
You can use rangeOfString function
NSRagne range = [string rangeOfString:#"!"];
This will give you an NSRange struct that gives you the position of the ! character.
NSUInteger pos = range.location;
Related
I have a NSString variable that contains "{265, 188}". I want just {265, 188} in a variable. What should I do?
Edit:
The code so far is:
//This I get from some XML so I change this part
NSDictionary* controlConditions =#{#"NSPosition":#"{265, 188}"};
NSString* a=(NSString*)[controlConditions valueForKey:#"AXPosition"];
After all, I need to send this to apple script set _controlid_ to a(variable) where i require it to be {265, 188}
Edit:
The variable controlConditions is taken from an XML that gives the position of a UIelement. The NSDictionary returns "{265, 188}" in a(variable). I need to send {265, 188} to an Applescript to match it to the position of various UIelements to get the right one. Hope this makes the problem clearer.
NSString *value = #"{265, 188}";
CGFloat x, y;
sscanf([value UTF8String], "{%lf, %lf}", &x, &y);
NSPoint point = NSMakePoint(x,y);
NSLog(#"%#", NSStringFromPoint(point));
So why don't you just put the values into a fixed array:
int anIntArray[2] = {265,188};
NSLog(#"anIntArray = %d, %d", anIntArray[0], anIntArray[1]);
prints:
anIntArray = 265, 188
Maybe you want to use some type other than int.
The question is really not clear. However, a string of the form {265, 188} is very possibly the result of a previous call to NSStringFromPoint() on an NSPoint whose x field is 265 and y field is 188.
Do you perhaps want to get the NSPoint value back out of the string? If so, you would pass the string to NSPointFromString().
That would not explain how quote characters actually got into the original string, if they are really there. (Unexpectedly, NSPointFromString() actually does still work with a string which contains quote characters.)
I need to gather data from a website based on the user's input.
searchString is the user inputted value, such as "search this string".
NSString *withoutSpaces = [searchString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Here, I need to replace spaces with %20
Next, I need to put the new string without spaces (replaced with %20) into another string.
NSString *unescapedSearchString = [NSString stringWithFormat:
#"website.com/query?=%22%#%22", withoutSpaces];
The site I need is not really "website.com", but that's just an example. I also need the %22 to remain at the beginning and end.
As you can see, I need the %# to format the new withoutSpaces user input into the website URL.
I did a search and found examples but I could not find any with formatting such as in my case using %#.
What's the best way to "escape" the characters and keep my formatted string? Currently, when I try to access data from the website, it comes back as null. However, when I try a string without the %# formatting and an actual value, I successfully retrieve the data from a website.
Any help is greatly appreciated.
You should do things this way:
NSString *searchString = ... // the raw search string with spaces and all
NSString *quoted = [NSString stringWithFormat:#"\"%#\"", searchString];
NSString *escaped = [quoted stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"website.com?query=%#&value=all", escaped];
BTW - the URL seems a little off. There should be a variable name before the = and after the ?.
I would like to search for a specific last character in a NSString and couldn't figure out a way. In C# for example, I can do lastindexof to get the index of the char I'm searching for and then use substring to get what I want. Is there a way in Obj C to achieve this?
NSString *s = #"aabbcc";
[s rangeOfString:#"b" options:NSBackwardsSearch]; // {3, 1}
I am trying to see if there is a short way of doing the following:
let s say I have the following NSString:
#"123456 copy cat";
I need to remove everything before "copy" and the length of the text before it is variable (depends on the user input)
I know that I can probably do an endless for loop to check the substring but I hope that there is a faster way
NSString *string = #"123456 copy cat";
NSRange range = [string rangeOfString:#"copy"];
NSString *newString = [string substringFromIndex:range.location];
That's the simple answer. The problem is that your user may enter the word 'copy', and then what? Even if you get the range this way:
NSRange range = [string rangeOfString:#"copy cat"];
there's still a chance your user may enter that phrase as well. By the time you put in additional checks, it's no longer a "short way."
EDIT
Don't overlook Chuck's excellent observation in the comments for using:
-[NSString rangeOfString:options:]
To remove part of a string you will either have to create a new string containing the part that you want to keep, or create a mutable copy of your string and modify that.
To create a new string containing the part starting with "copy":
NSString *input;
NSString *output;
NSRange copyRange;
input = #"123456 copy cat";
copyRange = [input rangeOfString:#"copy"];
output = [input substringFromIndex:copyRange.location];
To create a mutable string and remove the part up to "copy":
NSString *input;
NSMutableString *output;
NSRange copyRange;
input = #"123456 copy cat";
output = [input mutableCopy];
copyRange = [output rangeOfString:#"copy"];
[output replaceCharactersInRange:NSMakeRange(0, copyRange.location) withString:#""];
[output autorelease]; // depending on what you want to do
Please also see the comments in the other answer about using rangeOfString:withOptions: with NSBackwardsSearch in case the user input also contains the word "input".
I've been searching a solution for this problem, but it seems I'm the only one on the Internet who has encountered a problem like this.
I'm using the keychain wrapper class provided by Apple to store the user and password as it should be stored. When I want to get the user value back, is as easy as doing:
NSString *user = [keychain objectForKey:(id)kSecAttrAccount];
Retriveing the password should be as straightforward as the username:
NSString *pass = [keychain objectForKey:(id)kSecValueData];
But after that, trying to print them with an NSLog, nothing is shown on console AFTER the pass. For example:
NSLog(#"user: <%#>, pass: <%#>, something after the pass", user, pass);
The output of this NSLog is:
user: <123456>, pass: <5433
Invoking [pass length] gives me always a number greater than the actual length of the pass (in this example, 10, when I would say its length is actually 4).
I have no idea of what's going on. I made a workaround to patch this problem while I try to find a proper solution (looking every character's integer value and allowing only the ones which are numbers, letters and some symbols).
Thank you in advance!
The problem here is that you are trying to store a CFDataRef object as an NSString object, and then print it is a string using %#. It is a data object, not a string. If you'd like to see it as a string, you must first convert it into a string. Try something like the code below to convert it into a string before you try to log it:
NSData *passData = [keychain objectForKey:(id)kSecValueData];
NSString *pass = [[NSString alloc] initWithBytes:[passData bytes] length:[passData length] encoding:NSUTF8StringEncoding];
kSecValueData is of type: typedef const void * CFTypeRef;
You shouldn't typecast it to a NSString.
Try directly posting it into the NSLog like this.
NSLog(#"user <%#> pass <%#>", [keychain objectForKey:(id)kSecAttrAccount], [keychain objectForKey:(id)kSecValueData]);
Goodluck!