Here I need to truncate only the s letter in https can anyone help me how to truncate this from below string ?
https://192.168.1.11/magento2/pub/media/ewl_bannerslider/slides/5.jpg What you actually want is to change the scheme from "https" to "http" in an URL string. URLs can be safely manipulated using the URLComponents type:
var urlString = "https://192.168.1.11/magento2/pub/media/ewl_bannerslider/slides/5.jpg" if var urlComponents = URLComponents(string: urlString), urlComponents.scheme == "https" { urlComponents.scheme = "http" urlString = urlComponents.string! } print(urlString) // http://192.168.1.11/magento2/pub/media/ewl_bannerslider/slides/5.jpg If your intention is to create an URL request then you don't need the modified string, but only the URL(Request) with the changed scheme:
let urlString = "https://192.168.1.11/magento2/pub/media/ewl_bannerslider/slides/5.jpg" guard var urlComponents = URLComponents(string: urlString) else { // ... invalid URL string, bail out ... } if urlComponents.scheme == "https" { urlComponents.scheme = "http" } guard let url = urlComponents.url else { // ... invalid URL, bail out ... } let request = URLRequest(url: url) // ... It's easy to replace text inside a string with the help of the method replacingOccurrences(of:) it is available from swift2 onwards.
let originalStr = "https://192.168.1.11/magento2/pub/media/ewl_bannerslider/slides/5.jpg" let finalStr = originalStr.replacingOccurrences(of: "https", with: "http") print(finalStr) option2
let str = "https://192.168.1.11/magento2/pub/media/ewl_bannerslider/slides/5.jpg" if str.utf16.count >= 5{ let a = str.index(str.startIndex, offsetBy: 5) let result = String(str[..<a]) let replaced = str.replacingOccurrences(of: result, with: "http") print(replaced) }else{ //lenght in shorter } option3
var str = "https://192.168.1.11/magento2/pub/https/ewl_bannerslider/slides/5.jpg" str = str.stringByReplaceonFirstOccurrenceOfString(target: "https", withString: "http") print(str) extension String { func stringByReplaceonFirstOccurrenceOfString( target: String, withString replaceString: String) -> String { if let range = self.range(of: target) { return self.replacingCharacters(in: range, with: replaceString) } return self } } https in the string, it will be changed too, not just the one at the start."https://192.168.1.11/https/5.jpg" becomes "http://192.168.1.11/http/5.jpg", which is probably not intended.Try below code snippet:
let tmpStr = "https://192.168.1.11/magento2/pub/media/ewl_bannerslider/slides/5.jpg" let modifiedStr = tmpStr.replace("https", withString:"http") extension String { func replace(target: String, withString: String) -> String { return self.replacingOccurrences(of: target, with: withString, options: NSString.CompareOptions.literal, range: nil) } }
URLs asStrings. Do you think of cars on the level nuts and bolts? Simple operations (change the url's scheme fromhttptohttps) become much harder when you're at the wrong level abstraction (think in terms of aString, rather than aURL).