Multiple Substrings in String - objective-c

I´m working on an app that displays 6 pictures from a website. These pictures change over time so I extracted the sourcecode of said website and managed to pull the first of the 6 pictures with this code:
NSError *error = nil;
NSString *deviantStringPopular;
deviantStringPopular = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://browse.deviantart.com/?order=24"]
encoding:NSISOLatin1StringEncoding
error:&error];
NSString *popularContent;
NSRange popularURLRange1 = [deviantStringPopular rangeOfString:#"super_img=\""];
NSRange popularURLRange2 = [deviantStringPopular rangeOfString:#"\" super_w"];
int lengt = popularURLRange2.location - popularURLRange1.location - popularURLRange1.length;
int location = popularURLRange1.location + popularURLRange1.length;
NSRange endRange;
endRange.location = location;
endRange.length = lengt;
popularContent = [deviantStringPopular substringWithRange:endRange];
NSLog(#"%#", popularContent);
The problem is, the other 5 images' URLs are between the same substrings as the first one.
So is it possible that the first image's URL is ignored once the it´s loaded successfully and the second one loads and is stored under a different variable and so on?
thanks in advance

To get 6 URLs from the string and load them into different variables, you could try using a loop. With each iteration, after the substring is found and stored into a variable, make a range from the beginning of the string up to the end of the substring you found. You can use stringByReplacingCharactersInRange to erase the part of the string already searched so that the next time you search the string for the substring, it will find the next URL.

An easier solution may be to use RegexKit, if you know how to use RegEx's. Then each URL could just be a capture group.

Related

How to iterate through a string and find a certain character?

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;

How to Use An Escape URL String With Formatting in Objective-C

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 ?.

Finding extension of an URL

i have a string named url i.e
NSString *url = #"http://sound17.mp3pk.com/indian/barfi/%5BSongs.PK%5D%20
Barfi%20-%2001%20-%20Barfi!.mp3";
Now I want that It should search from last upto the .(dot)
i.e it should search
mp3 in string as it is coming after .(dot) and want to store that mp3 in the temporary variable.
how can i use lastindex(".") or something else to store in temporary variable.
Simple as that:
NSString *extension = [url pathExtension];

Scripting Bridge objective-c PagesWord

I'm trying to modify an existing Pages document from within my application using the Scripting Bridge. I've followed all steps mentioned in the documentation: I created a header file and thoroughly examined it, but I just can't figure out how how to do this.
I'm basically trying to do a search an replace: I've got a list of strings and I want to replace some search values with those strings. The problem is that I just can't figure out how the PagesWord class works. I just want to get a string from it and it compare it to my search value. I currently have to following code:
PagesApplication *pages = [SBApplication applicationWithBundleIdentifier:#"com.apple.iWork.Pages"];
PagesDocument *document = [pages open:inputURL];
PagesText *bodyText = [document bodyText];
SBElementArray *words = [bodyText words];
NSLog([NSString stringWithFormat:#"%d words.", [words count]]);
for (PagesWord *word in [bodyText words]) {
NSLog((NSString *)word);
}
Everything works well until the last 3 rows: the correct Pages document is opened and the word count is logged, but the string isn't: I just see exception messages. I also tried to work with the properties of PagesWord, but I have the same problems then...
Can anyone help me?
To replace the word i use:
for (PagesWord *word in [bodyText words]) {
[word select];
[[document selection] setTo:#"my new value"];}

how to get first three characters of an NSString?

How can I return the first three characters of an NSString?
mystr=[mystr substringToIndex:3];
Be sure your string has atleast 3 ch.. o.e. it will crash the app.
Here are some other links to check NSsting operations...
Link1
Link2
Apple Link
First, you have to make sure that the string contains at least 3 characters:
NSString *fullString = /* obtain from somewhere */;
NSString *prefix = nil;
if ([fullString length] >= 3)
prefix = [fullString substringToIndex:3];
else
prefix = fullString;
substringToIndex: will throw an exception if the index you provide is beyond the end of the string.
the right way is:
text = [text substringToIndex:NSMaxRange([text rangeOfComposedCharacterSequenceAtIndex:2])];
substringToIndex of NSString is indexing by code unit, emoji takes two code units.
make sure check the index yourself.