0

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 
1
  • 2
    Stop thinking of URLs as Strings. Do you think of cars on the level nuts and bolts? Simple operations (change the url's scheme from http to https) become much harder when you're at the wrong level abstraction (think in terms of a String, rather than a URL). Commented Oct 5, 2017 at 6:01

3 Answers 3

5

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) // ... 
Sign up to request clarification or add additional context in comments.

Comments

-1

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 } } 

3 Comments

Please note that if there happens to be another instance of https in the string, it will be changed too, not just the one at the start.
may I know the reason of downvote, answer prodces multiple ways, if i made the mistake it will better to update
Not my downvote, but note that both your solutions will replace all occurrences of "https" in the string by "http", not only the initial scheme part. For example, "https://192.168.1.11/https/5.jpg" becomes "http://192.168.1.11/http/5.jpg", which is probably not intended.
-1

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) } } 

Comments