103

I am trying to do IFSCCode validation in swift, but the problem I'm facing is I am not able to fetch the first four letters in string.

IFSC Code example:

ABCD0200000

This is how an IFSC Code looks:

  1. First four characters in IFSC Code are always alphabets
  2. Fifth character is always a zero.
  3. And rest can be anything
  4. The length of IFSC Code should not be greater than or less than 11. It should 11 in length.

I have written code for ifs code validation in Objective C, but I am not that familiar in Swift so getting a problem in replicating the same in Swift.

The following is the code which I have written in Objective C:

- (BOOL)validateIFSCCode:(NSString*)ifsccode {
  if (ifsccode.length < 4) {
    return FALSE;
  }
  for (int i = 0; i < 4; i++) {
    if (![[NSCharacterSet letterCharacterSet]
            characterIsMember:[ifsccode characterAtIndex:i]]) {
      return FALSE;
    }
  }
  if (ifsccode.length < 10) {
    return FALSE;
  }
  if ([ifsccode characterAtIndex:4] != '0') {
    return FALSE;
  } else {
    return TRUE;
  }
}

In Swift 3

func validateIfscCode(_ ifscCode : String) -> Bool{
    if(ifscCode.characters.count < 4){
        return false;
    }

    for( _ in 0 ..< 4){
        let charEntered = (ifscCode as NSString).character(at: i)
    }
    if(ifscCode.characters.count < 10){
        return false;
    }

    let idx = ifscCode[ifscCode.index(ifscCode.startIndex, offsetBy: 4)]

    print("idx:%d",idx)
    if (idx == "0"){
    }
    return true
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Rani
  • 3,333
  • 13
  • 48
  • 89

4 Answers4

226

The easiest way to get a string's prefix is with

// Swift 3
String(string.characters.prefix(4))

// Swift 4 and up
string.prefix(4)

// Objective-C
NSString *firstFour = [string substringToIndex:4];
Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
  • I'm getting 'Cannot assign value of type 'String.SubSequence' (aka 'Substring') to type 'String?'' – etayluz May 24 '18 at 21:30
  • 13
    @etayluz If you need the result to be a `String` then you'll need to convert the substring to a regular string with `String(string.prefix(4))`. – Tim Vermeulen May 25 '18 at 06:32
77

This is a simple validation using regular expression. The pattern represents:

  • ^ must be the beginning of the string
  • [A-Za-z]{4} = four characters A-Z or a-z
  • 0one zero
  • .{6} six arbitrary characters
  • $ must be the end of the string

Updated to Swift 4

func validateIFSC(code : String) -> Bool {
  let regex = try! NSRegularExpression(pattern: "^[A-Za-z]{4}0.{6}$")
  return regex.numberOfMatches(in: code, range: NSRange(code.startIndex..., in: code)) == 1
}

PS: To answer your question, you get the first 4 characters in Swift 3 with

let first4 = code.substring(to:code.index(code.startIndex, offsetBy: 4))

and in Swift 4 simply

let first4 = String(code.prefix(4))
vadian
  • 274,689
  • 30
  • 353
  • 361
20

Swift 5

string.prefix(4)  // Replace 4 with number of letters you want to get from the string

This is working on Xcode 10.2.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
8

Extention to get all substring

extension String {
   func first(char:Int) -> String {
        return String(self.prefix(char))
    }

    func last(char:Int) -> String
    {
        return String(self.suffix(char))
    }

    func excludingFirst(char:Int) -> String {
        return String(self.suffix(self.count - char))
    }

    func excludingLast(char:Int) -> String
    {
         return String(self.prefix(self.count - char))
    }
 }
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84