I have some string that have a type like this
NSString *string1 = #"/Users/mine/Library/Application Support/iPhone Simulator/3.2/Applications/02221798-1B7A-46B4-928F-F5BE37E177B5/Documents/Project/Project 1/Folder 1/2.php";
if i just want to get the "02221798-1B7A-46B4-928F-F5BE37E177B5"
i have implement a code like this :
NSString *subString1;
int count = [string1 length];
NSLog(#"count : %d", count);
subString1 = [string1 substringFromIndex:121];
but it left "02221798-1B7A-46B4-928F-F5BE37E177B5/Documents/Project/Project 1/Folder 1/2.php"
how can i do to fix it??
To get the name of your app's parent directory you should try this instead:
NSString *appUUID = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] lastPathComponent];
This will work both on a device and Simulator.
If you need to get the full path to the Documents folder inside your app sandbox:
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
If you need to append a path component to a path string:
NSString *projectPath = [documentsPath stringByAppendingPathComponent: #"Project"];
Please, take a look at NSString documentation, section "Tasks", "Working with Paths". It is a must-read.
you can use stringWithRange method of NSString.
subString1 = [string1 stringWithRange:NSMakeRange(startPos,yourLengthToSelect)];
or you can use componentSeparetedByString and break string using #"/". it will return array of string. get your desired string using array index.
Related
for(int i= 0 ;i<[urlsArrray count]; i++)
{
NSString *urlString = [urlsArrray objectAtIndex:i];
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escapedUrlString];
NSString *urlstring1 = [url absoluteString];
NSArray *parts = [urlstring1 componentsSeparatedByString:#"/"];
NSString *fileName = [parts objectAtIndex:[parts count]-1];
NSMutableString *tempString = [NSMutableString stringWithString:fileName];
// [tempString replaceCharactersInRange:[tempString rangeOfString:#"%20"] withString:#" "];
NSLog(#"file name in temp string: %# word name: %#", tempString, wordNameDB);
NSRange match = [tempString rangeOfString:wordNameDB];
if(match.location != NSNotFound)
{
NSLog(#"match found at %u", match.location);
isAvailable = YES;
break;
}
Hi friends, now my problem is i am getting file name from server..., if file name is having any spaces then it replace '%20' ( i.e ex: "hello world" is actual name but i am getting file name like: "hello%20world") .
1. I am not sure all file names having spaces.
2. And also i am not sure a file may have only one space
so first i have to check the file is having spaces or not, if have then i want to replace all "%20" with #" " string. Please give me any suggestions or code snippets.
OR " THERE IA ANY OTHER WAY TO READ FILE NAMES WITHOUT GETTING '%20' IN THE PLACE OF SPACE(#" ")..... thank you
If you have your file name stored in fileName param, you can use the following:
fileName = [fileName stringByReplacingOccurrencesOfString:#"%20" withString:#" "];
The above code will replace all "%20" with " ". If there are no "%20" in the fileName, you will get back the same string.
Correction:
I was confused with stringByAddingPercentEscapesUsingEncoding mentioned in code and thought you have already used stringByReplacingPercentEscapesUsingEncoding. If you are not using stringByReplacingPercentEscapesUsingEncoding method, you should use that in this case. The above code is useful, only if that is not able to remove any particular string which you want to replace.
What you need is replacing the escape charcters, according to the encoding.
Use this and all your spaces and other URL encoded characters will be converted to what you need.
[#"yourString" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
THERE IA ANY OTHER WAY TO READ FILE NAMES WITHOUT GETTING '%20' IN THE PLACE OF SPACE(#" ")
Yes, use this:
NSString *newString = [yourstring stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Use this to remove spaces ..
urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
You seem to already have a valid NSURL object representing the file. Getting the filename from a URL is easy:
...
NSURL *url = [NSURL URLWithString:escapedUrlString];
NSString *path = [url path];
NSString *filename = [path lastPathComponent];
No fiddling with unescaping percent escapes, URL parsing, and other error prone stuff.
I am new to iphone.I have small doubt that is I have a path of my audiofile which is placed in the directory in resources folder that path is
/Users/Chary/Library/Application Support/iPhone Simulator/5.0/Applications/B02404E5-52DC-49B6-8DBB-C9946E4331AF/BiblePlayer.app/raj/1.mp3
My question is how to retrieve the string "1.mp3" from that entire path?
NSString *fullPath = #"…";
NSString *fileName = [fullPath lastPathComponent];
NSString *allString = #"/Users/Chary/Library/Application Support/iPhone Simulator/5.0/Applications/B02404E5-52DC-49B6-8DBB-C9946E4331AF/BiblePlayer.app/raj/1.mp3"
int slashPosition = [allString rangeOfString:#"/" options:NSBackwardsSearch].location;
NSString *function = [allString substringFromIndexslashPosition + 1];
This will get you the path of the file after the last slash
try it:-
NSString *yourPath = #"/Users/Chary/Library/Application Support/iPhone Simulator/5.0/Applications/B02404E5-52DC-49B6-8DBB-C9946E4331AF/BiblePlayer.app/raj/1.mp3";
NSString *fileName = [yourPath lastPathComponent];
It may help u thank :)
My NSArray is:
self.files = [bundle pathsForResourcesOfType:#"ppt" inDirectory:#"thepowerpoints"];
This returns the full path of
/User/Name/.../filename.ppt
I can use a NSString to get only the filename using:
NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
Is there a way that I can cleanup the original NSArray to only return the lastPathComponent without the extension?
Am needing to do this to search within the tableview array. Currently, if a file is named "Test.ppt" the search bar will show it if I type in "/User/Name", because the Array it is searching includes the entire path.
I would prefer for it to only search the filename.
NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count]];
for (NSString *path in self.files) {
[names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
}
self.files = names;
I am trying to find out if a document exists by running
NSString *DocumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *theFile = [[DocumentPath stringByAppendingPathComponent:FileName] retain];
NSLog(#"File : %#",theFile);
NSLog(#"FileName : %#", FileName);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:theFile];
NSLog(#"FileExists : %#", fileExists);
I know that the document is there, but I want to run this to find out and if say its been deleted are something then do something else. But all I get as my NSLog is
FileExists : (null)
I have looked at some tutorials and they say this is right. I'm I missing something?
This code work for me. saving file in Resources->file.txt
NSString *myFilePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/file.txt"];
NSLog(#"FileExist %d",[[NSFileManager defaultManager] fileExistsAtPath:myFilePath]);
Use this line instead of your's:
NSLog(#"FileExists : %d", fileExists);
%# format specifier is used for objects only, and prints in result of description method call for specified object
I coded a method to load a txt file into an array. However, I'm not really happy with it as it looks terribly cumbersome to my beginner's eyes (I'm sure I don't need 50% of my code) and I am somehow wondering how I can specify the exact format of my txt file, e.g. NSUTF8StringEncoding.
Here is my code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Sample.txt"];
if (filePath) { // check if file exists - if so load it:
NSString *myText = [NSString stringWithContentsOfFile:filePath];
if (myText) {textView.text=myText;}
}
For any suggestions of how to polish this up and specify the right format, I'd be very grateful.
Try the following, assuming your file is in your bundle:
NSString * filePath = [[NSBundle mainBundle] pathForResource:#"Sample" ofType:#"txt"];
NSError * error = nil;
NSString * contentsOfFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
Into an array? You mean into a string, since is exactly what you're doing...
However, your code looks not bad, and most of it is just to grab the documents directory path, and that's not your fault, since it's exactly done this way, according to many knowledge bases.
As of the encoding, stringWithContentsOfFile is deprecated, please use stringWithContentsOfFile:encoding:error: (see the docs)
and you will be able to specify the correct encoding and get accurate error descriptions.