Swift StringBetweenString Function

I am trying to make a simple quick app for iOS. I am new to Swift and iOS. I need to get a substring between two lines from my text. For example, I have the text " http://google.com " and I want to get a substring between ": //" and ".".

I do not know how I can do this.

I am trying to use regular expressions, but I think this is bad.

TY for answers and sorry for my english.

+4
source share
4 answers

Several variants:

  • Regular expressions work well. See ICU User Guide: Regular Expressions

    Example:

    let us = "http://google.com"
    let range = us.rangeOfString("(?<=://)[^.]+(?=.)", options:.RegularExpressionSearch)
    if range != nil {
        let found = us.substringWithRange(range!)
        println("found: \(found)") // found: google
    }
    

    Notes:

        (?<=://) means preceded by ://  
        [^.]+    means any characters except .  
        (?=.)    means followed by .  
    
  • NSScanner . . Apple NSScanner

    :

    let us = "http://google.com"
    let scanner = NSScanner(string:us)
    var scanned: NSString?
    
    if scanner.scanUpToString("://", intoString:nil) {
        scanner.scanString("://", intoString:nil)
        if scanner.scanUpToString(".", intoString:&scanned) {
            let result: String = scanned as String
            println("result: \(result)") // result: google
        }
    }
    
+15

:.//.+

:.//Google

:

var yourURL: NSString = "http://google.com" // this is your input and could be any URL
var regex: NSRegularExpression = NSRegularExpression.regularExpressionWithPattern("://.+\\.", options: NSRegularExpressionOptions.fromMask(UInt(0)), error: nil) // need double backspace because of backspace in String is \\ not \
var needleRange = regex.rangeOfFirstMatchInString(yourURL, options:NSMatchingOptions.Anchored, range: NSMakeRange(0, yourURL.length))
var needle: NSString = yourURL.substringWithRange(needleRange)

3 ,

Google

:

import Foundation

var halfURL: NSString = "://google."
var prefix: NSString = "://"
var suffix: NSString = "."
var needleRange: NSRange =  NSMakeRange(prefix.length, halfURL.length - prefix.length -     suffix.length)
var needle: NSString = halfURL.substringWithRange(needleRange)
// needle is now 'google'
+2

If your input is a valid URL, you can use the class NSURLto do the parsing for you:

var result : NSString?
let input = "http://test.com/blabla"

// Parse the string; might fail
let url : NSURL? = NSURL(string: input)

// Get the host part of the URL ("test.com")
let host = url?.host

// Split it up at the dots.
let hostParts = host?.componentsSeparatedByString(".")

// Assign the first part of the hostname if we were successful up to here.
if hostParts?.count > 0 {
    result = hostParts![0]
}

Bonus: ignore "www":

if hostParts?.count > 0 {
    if (hostParts![0] == "www" && hostParts!.count > 1) {
        result = hostParts![1]
    } else {
        result = hostParts![0]
    }
}
+1
source

For quick 3.0:

let us = "http://example.com"
let range = us.range(of:"(?<=://)[^.]+(?=.com)", options:.regularExpression)
if range != nil {
    let found = us.substring(with: range!)
    print("found: \(found)") // found: example
}
0
source

All Articles