0

I have a url that looks like this :

https://google.com/image/ghwUT23Y.jpeg

I want to be able to achieve something like this:

section = image; 
value = ghwUT23Y; 

I have tried to explode the url and usecomponentsSeperatedByStringmethod to grab the values I need.

 if ([[explodedUrl firstObject] containsString:@"google.com"] && ![[explodedUrl firstObject] isEqualToString:@"https://google.com/"]) {

    NSString *sectionString = [[[explodedUrl lastObject] componentsSeparatedByString:@"/"] firstObject];
    NSLog(@"redirectttttt: %@",sectionString);
    NSString *itemString = [[[explodedUrl lastObject] componentsSeparatedByString:@"/"] lastObject];
    NSLog(@"redirectttttt:2 %@",itemString);

Problem:

Using this method, itemString returns me the value : ghwUT23Y.jpegbut sectionString returns me https://

How would I be able to get image as a section?

  • 1
    I would use `NSURLComponents` to get the components of an url. – dasdom Nov 24 '17 at 09:26
  • Possible duplicate of [How to split filename from file extension in Swift?](https://stackoverflow.com/questions/26707352/how-to-split-filename-from-file-extension-in-swift) – Kiran Sarvaiya Nov 24 '17 at 09:32
  • 2
    `NSURL *url = [NSURL URLWithString:@"https://google.com/image/ghwUT23Y.jpeg"]; NSArray *components = [url pathComponents]; NSString *imageSection = [components objectAtIndex:1]; NSString *imageName = [[components lastObject] stringByDeletingPathExtension];` should do the trick. I used direct index, but should be modified as needed. – Larme Nov 24 '17 at 09:33
  • 1
    Check this: `NSString * url = @"https://google.com/image/ghwUT23Y.jpeg"; if ([url rangeOfString:@"image"].location != NSNotFound) { NSArray *components = [url componentsSeparatedByString:@"/"]; NSString * section = components[components.count - 2]; NSString * value = [components lastObject]; NSLog(@"%@ %@",section, value); }` – biloshkurskyi.ss Nov 24 '17 at 09:37
  • @Larme worked perfectly, thanks –  Nov 24 '17 at 09:45

2 Answers2

2

You need to convert your string to url first and use the pathComponents method to access each components of that url.

Objective C:

NSURL *url                 = [NSURL URLWithString:@"ttps://google.com/image/ghwUT23Y.jpeg"];
NSMutableArray *components = [[url pathComponents] mutableCopy];
NSString *fileName         = [components lastObject];
[components removeLastObject];
NSString *section          = [components lastObject];
NSLog(@"File : %@, Section: %@",fileName, section);

Swift

let url        = URL(string: "https://google.com/image/ghwUT23Y.jpeg")
var components = url?.pathComponents
let fileName   = components?.popLast()
let section    = components?.popLast()
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

Swift :

func detectUrl()  {
    let strLongDescription = "https://google.com/image/ghwUT23Y.jpeg"

    if(strLongDescription.contains("image")){

        var arrString : [String] = strLongDescription.components(separatedBy: "google.com/")
        let lastOfarrString : String = arrString.last!
        arrString = lastOfarrString.components(separatedBy: "/")
        let section = arrString.first!
        let value = arrString.last! 
    }
}
Nilomi Shah
  • 186
  • 7