Skip to content

Instantly share code, notes, and snippets.

@jerrypm
Last active October 11, 2018 03:35
Show Gist options
  • Select an option

  • Save jerrypm/0dd59a4f538b3fa2fd4fdb379b4b2a67 to your computer and use it in GitHub Desktop.

Select an option

Save jerrypm/0dd59a4f538b3fa2fd4fdb379b4b2a67 to your computer and use it in GitHub Desktop.
// NumberBeforeStringVC.swift
// For Test Part 4
//
// Created by Jeri PM on 05/10/18.
// Copyright © 2018 Jeri PM. All rights reserved.
//
class NumberStringVC: UIViewController {
@IBOutlet weak var texField: UITextField!
@IBOutlet weak var texField1: UITextField!
var noPolisi = "8923ZE"
override func viewDidLoad() {
super.viewDidLoad()
let strint = noPolisi.group(of: 4)
print("\(strint[0]) Or \(strint[1])")
self.texField.text = "\(strint[0])"
self.texField1.text = "\(strint[1])"
}
}
// Group String
extension String {
func group(of n: Int) -> [String] {
let chars = Array(self)
return stride(from: 0, to: chars.count, by: n).map {
String(chars[$0..<min($0+n, chars.count)])
}
}
}
//-----------------------------------------------------------------------
// or can use this, simple code
class NumberStringVC: UIViewController {
@IBOutlet weak var texField: UITextField!
@IBOutlet weak var texField1: UITextField!
var noPolisi = "8923_ZE" // use "_" to split by components
override func viewDidLoad() {
super.viewDidLoad()
let strint = noPolisi.components(separatedBy: "_")
print("\(strint[0]) Or \(strint[1])")
self.texField.text = "\(strint[0])"
self.texField1.text = "\(strint[1])"
}
}
@jerrypm
Copy link
Copy Markdown
Author

jerrypm commented Oct 5, 2018

Split String And Set Index

My Blog -> Scrip Tes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment