Crop NSURL / NSString - iphone

I got some URL's heading to certain mp3's like:
(1) localhost://blablabla/song1.mp3
(2) localhost://blablabla/songwithmorechars.mp3
and so on.
How can I crop the URL's to:
(1) song1.mp3
(2) songwithmorechars.mp3
Need to display the current song my AVAudioPlayer is playing in a UILabel.
Thanks
SOLUTION:
Here's the deal:
titleLabel.text = [[[self.audioPlayer.url absoluteString] lastPathComponent] stringByReplacingOccurrencesOfString:#".mp3" withString:#""];

Take the substring with the last / (there's a method in ObjC for that!)
NSString *sub = [url lastPathComponent];
Here's the info for that method:
NSString lastPathComponent Apple Doc

Just use [url lastPathComponent], you don't need to convert it to a string first.

use
NSString *lastString = [yourStringName lastPathComponent];

NSURL *firstURL = [NSURL URLWithString:#"localhost://blablabla/song1.mp3"];
NSString *firstString = [firstURL absoluteString];
NSLog(#"Name:%#",[firstString lastPathComponent]);
From docs:
NSURL Class Reference
NSString Class Reference

Related

how to replace %# with a nsstring variable? iPhone

How to replace the following correctly?
NSString *stringURL = [NSString stringWithFormat:#"http://192.168.1.183:8001/GetDocument.aspx?id=%# & user=admin_document",self.index];
NSURL *targetURL = [NSURL URLWithString:stringURL];
I want to replace %# with self.index.
Your problem is more likely the whitespace. Try removing it:
NSString *stringURL = [NSString stringWithFormat:#"http://192.168.1.183:8001/GetDocument.aspx?id=%#&user=admin_document", self.index];
NSLog(#"URL:%#", stringURL); //You can print out to check
NSURL *targetURL = [NSURL URLWithString:stringURL];
Remove the whitespaces in the link.
If self.index is not an NSString object, then the %# format specifier will not work. I suspect with a name like index it is probably an NSInteger or similar, in which case you want to use %d instead.
Have a look at this Apple documentation for all the possible format specifiers and the types they correspond to. The compiler should pick you up on this though and suggest the correct specifier.
just parse and use it if it is integer then you use like this
int a=10;
NSString *stringURL = [NSString stringWithFormat:#"this is my number %d",a];
and if you want to use string then you did like this
NSString stringName=#"john";
NSString *stringURL = [NSString stringWithFormat:#"this is my name %#",stringName];

NSURL and NSString converting issue

When trying to load a request I do like this:
NSString *urlStrings = [NSLocalizedStringFromTable(#"kPicturesURL", #"urls", nil) stringByAppendingPathComponent:#"Articles/a.pdf"];
NSURL *url = [NSURL URLWithString:urlStrings];
Inside of "urls" I have this key:
/* Service pictures directory */
"kPicturesURL" = "http://192.168.2.104/myApp/Pictures";
And I get this result: (The first line is "urlStrings" and the second is the "url")
(NSString *) $6 = 0x092a8f60 http:/192.168.2.104/myApp/Pictures/Articles/a.pdf
2012-11-22 09:18:20.093 NPE[7680:c07] Couldn't issue file extension for path: /192.168.2.104/myApp/Pictures/Articles/a.pdf
I've tried those questions:
NSString and NSUrl not converting properly
NSURL not getting allocatd with NSString
Pass NSString into NSURL URLWithString ?
NSString to NSURL ?
NSString to NSURL
Non of those worked, what seems to be the problem?
Thanks!
Maybe instead of:
http:/192.168.2.104/myApp/Pictures/Articles/a.pdf
This:
http://192.168.2.104/myApp/Pictures/Articles/a.pdf
So, using one of the following did worked eventually.
This one:
NSString *stringURL = [url absoluteString];

How to get find and get URL in a NSString in iPhone?

I have a text with http:// in NSString. I want to get that http link from the NSString. How can i get the link/url from the string? Eg: 'Stack over flow is very useful link for the beginners https://stackoverflow.com/'. I want to get the 'https://stackoverflow.com/' from the text. How can i do this? Thanks in advance.
I am not sure what you exactly mean by link but if you want to convert your NSString to NSURL than you can do the following:
NSString *urlString = #"http://somepage.com";
NSURL *url = [NSURL URLWithString:urlString];
EDIT
This is how to get all URLs in a given NSString:
NSString *str = #"This is a grate website http://xxx.xxx/xxx you must check it out";
NSArray *arrString = [str componentsSeparatedByString:#" "];
for(int i=0; i<arrString.count;i++){
if([[arrString objectAtIndex:i] rangeOfString:#"http://"].location != NSNotFound)
NSLog(#"%#", [arrString objectAtIndex:i]);
}
Rather than splitting the string into an array and messing about that way, you can just search for the substring beginning with #"http://":
NSString *str = #"Stack over flow is very useful link for the beginners http://stackoverflow.com/";
// get the range of the substring starting with #"http://"
NSRange rng = [str rangeOfString:#"http://" options:NSCaseInsensitiveSearch];
// Set up the NSURL variable to hold the created URL
NSURL *newURL = nil;
// Make sure that we actually have found the substring
if (rng.location == NSNotFound) {
NSLog(#"URL not found");
// newURL is initialised to nil already so nothing more to do.
} else {
// Get the substring from the start of the found substring to the end.
NSString *urlString = [str substringFromIndex:rng.location];
// Turn the string into an URL and put it into the declared variable
newURL = [NSURL URLWithString:urlString];
}
try this :
nsstring *str = #"Stack over flow is very useful link for the beginners http://stackoverflow.com/";
nsstring *http = #"http";
nsarray *arrURL = [str componentsSeparatedByString:#"http"];
this will give two objects in the nsarray. 1st object will be having:Stack over flow is very useful link for the beginners and 2nd will be : ://stackoverflow.com/ (i guess)
then you can do like:
NSString *u = [arrURL lastObject];
then do like:
nsstring *http = [http stringByAppendingFormat:#"%#",u];
Quite a lengthy,but i think that would work for you. Hope that helps you.

Concatenating Objective-C NSStrings

I'm a total newbie at Objective-C, so bear with me. This is how I'm concatenating my URL:
id url = [NSURL URLWithString:#"http://blahblah.com/gradient.jpg"];
id image = [[NSImage alloc] initWithContentsOfURL:url];
id tiff = [image TIFFRepresentation];
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent: #"Desktop"];
NSString *fileToWrite = #"/test.tiff";
NSString *fullPath = [docsDir stringByAppendingString:fileToWrite];
[tiff writeToFile:fullPath atomically:YES];
It works, but it seems sloppy. Is this the ideal way of doing concatenating NSStrings?
stringByAppendingString: or stringWithFormat: pretty much is the way.
You can append multiple path components at once. E.g.:
NSString* fullPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Desktop/test.tiff"];
You can also specify the entire path in a single string:
NSString* fullPath = [#"~/Desktop/test.tiff" stringByExpandingTildeInPath];
Have you looked into NSMutableString ?
A common convention is to use [NSString stringWithFormat:...] however it does not perform path appending (stringByAppendingPathComponent).

function to get the file name of an URL

I have some source code to get the file name of an url
for example:
http://www.google.com/a.pdf
I hope to get a.pdf
because the way to join 2 NSStrings I can get is 'appendString' which only for adding a string at right side, so I planned to check each char one by one from the right side of string 'http://www.google.com/a.pdf', when it reach at the char '/', stop the checking, return string fdp.a , after that I change fdp.a to a.pdf
source codes are below
-(NSMutableString *) getSubStringAfterH : originalString:(NSString *)s0
{
NSInteger i,l;
l=[s0 length];
NSMutableString *h=[[NSMutableString alloc] init];
NSMutableString *ttt=[[NSMutableString alloc] init ];
for(i=l-1;i>=0;i--) //check each char one by one from the right side of string 'http://www.google.com/a.pdf', when it reach at the char '/', stop
{
ttt=[s0 substringWithRange:NSMakeRange(i, 1)];
if([ttt isEqualToString:#"/"])
{
break;
}
else
{
[h appendString:ttt];
}
}
[ttt release];
NSMutableString *h1=[[[NSMutableString alloc] initWithFormat:#""] autorelease];
for (i=[h length]-1;i>=0;i--)
{
NSMutableString *t1=[[NSMutableString alloc] init ];
t1=[h substringWithRange:NSMakeRange(i, 1)];
[h1 appendString:t1];
[t1 release];
}
[h release];
return h1;
}
h1 can reuturn the coorect string a.pdf, but if it returns to the codes where it was called, after a while system reports
'double free
*** set a breakpoint in malloc_error_break to debug'
I checked a long time and foudn that if I removed the code
ttt=[s0 substringWithRange:NSMakeRange(i, 1)];
everything will be Ok (of course getSubStringAfterH can not returns the corrent result I expected.), no error reported.
I try to fix the bug a few hours, but still no clue.
Welcome any comment
Thanks
interdev
The following line does the job if url is a NSString:
NSString *filename = [url lastPathComponent];
If url is a NSURL, then the following does the job:
NSString *filename = [[url path] lastPathComponent];
Try this:
Edit: from blow comment
NSString *url = #"http://www.google.com/a.pdf";
NSArray *parts = [url componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
I think if you have already had the NSURL object, there is lastPathComponent method available from the iOS 4 onwards.
NSURL *url = [NSURL URLWithString:#"http://www.google.com/a.pdf"];
NSString *filename = [url lastPathComponent];
Swift 3
Let's say that your url is http://www.google.com/a.pdf
let filename = url.lastPathComponent
\\filename = "a.pdf"
This is more error free and meant for getting the localized name in the URL.
NSString *localizedName = nil;
[url getResourceValue:&localizedName forKey:NSURLLocalizedNameKey error:NULL];
I haven't tried this yet, but it seems like you might be trying to do this the hard way. The iPhone libraries have the NSURL class, and I imagine that you could simply do:
NSString *url = [NSURL URLWithString:#"http://www.google.com/a.pdf"];
NSString *path = [url path];
Definitely look for a built in function. The libraries have far more testing and will handle the edge cases better than anything you or I will write in an hour or two (generally speaking).