remy boys remy boys - 3 months ago 9
iOS Question

Parse a string with specific condition

how can i trim my string which is in this forms e.g. :

https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg


to this

media/93939393hhs8.jpeg


what i want is to remove the remaining characters from second
/
slash (from backward)

i can use
stringByTrimmingCharactersInSet
but i dont know how to specify here the condition that i want

let trimmedString = myString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet() // what here in my case ??
)


the above is for removing the white spaces , but that not the case here

p.s. if my question is not clear enough then let me know i'll explain it in a little bit more deep

Answer

Since the string is an URL get the path components, remove anything but the last 2 items and join the items with the slash separator.

if let url = NSURL(string:"https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg"), pathComponents = url.pathComponents {
  let trimmedString = pathComponents.suffix(2).joinWithSeparator("/")
  print(trimmedString)
}
Comments