How to masking the last number in Swift? - swift

How to mask the last string using swift, I have made the code as below. but the code only shows the last number, my expectation is that the code displays the first 5 digits
here my code:
extension StringProtocol {
var masked: String {
return String(repeating: "•", count: Swift.max(0, count-5)) + suffix(5)
} }
var name = "0123456789"
print(name.masked)
I get output: •••••56789
but my expectations: 01234•••••

Use a prefix instead of a suffix
extension StringProtocol {
var masked: String {
return prefix(5) + String(repeating: "•", count: Swift.max(0, count-5))
}
}
You could also create a function instead for parameterizing the number of digits and direction (or even the mask character)
extension StringProtocol {
func masked(_ n: Int = 5, reversed: Bool = false) -> String {
let mask = String(repeating: "•", count: Swift.max(0, count-n))
return reversed ? mask + suffix(n) : prefix(n) + mask
}
}
var name = "0123456789"
print(name.masked(5))
// 01234•••••
print(name.masked(5, reversed: true))
// •••••56789

If you have a case of wanting to mask an email address. here's the code
func maskingEmail(email: String) -> String {
let emailComponents = email.components(separatedBy: "#")
let emailDomainComponents = emailComponents[1].components(separatedBy: ".")
let maskedEmailName = String(repeating: "•", count: Swift.max(0, emailComponents[0].count-3)) + emailComponents[0].suffix(3)
let maskedEmailProvider = String(repeating: "•", count: Swift.max(0, emailDomainComponents[0].count-3)) + emailDomainComponents[0].suffix(3)
let emailDomain = emailDomainComponents[1]
return "\(maskedEmailName)#\(maskedEmailProvider).\(emailDomain)"
}
// The Output
print(maskingEmail(email: "pr1vaterelay#gmail.com")) // •••••••••lay#••ail.com
print(maskingEmail(email: "private_relay#bk.ru")) // ••••••••••lay#bk.ru
print(maskingEmail(email: "private.relay#protonmail.com")) // ••••••••••lay#•••••••ail.com

Related

How can i mask the first and last characters in Swift?

I have an extension to mask the first and last characters. When I try to mask last characters it works but if I try to mask the first characters it doesn't mask as I want to.
For example;
extension String {
func maskedChar(_ charCount: Int, lastCharacters: Bool = true) -> String {
if self.count <= charCount {
return self
}
let mask = String(repeating: "*", count: charCount)
return lastCharacters ? self.prefix(self.count - charCount) + mask : self.suffix(self.count - charCount) + mask
}
}
let sample = "123456"
print(sample.maskedChar(3))
print(sample.maskedChar(3, lastCharacters: false))
// 123***
// 456***
What I want to see is basically ***456.
And also, is there any way to shorten the code? The return line is too long to see it.
Pretty simple, really. You just need to reverse the order of the characters from the string and the mask in the "not lastCharacters" case:
extension String {
func maskedChar(_ charCount: Int, lastCharacters: Bool = true) -> String {
if self.count < charCount {
return String(repeating: "*", count: self.count)
}
let mask = String(repeating: "*", count: charCount)
if lastCharacters {
return self.prefix(self.count - charCount) + mask
} else {
return mask + self.suffix(self.count - charCount)
}
}
}
Note that I also changed the if clause at the beginning. It seems to me you should ALWAYS mask the data, even if the source string is too short.
I would use dropFirst/dropLast together with the mask you create
extension String {
func maskedChar(_ charCount: Int, lastCharacters: Bool = true) -> String {
if self.count <= charCount { return self }
let mask = String(repeating: "*", count: charCount)
return lastCharacters ? self.dropLast(charCount) + mask : mask + self.dropFirst(charCount)
}
}

substring(with:)' is deprecated: Please use String slicing subscript [duplicate]

I have the following simple code written in Swift 3:
let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)
From Xcode 9 beta 5, I get the following warning:
'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.
How can this slicing subscript with partial range from be used in Swift 4?
You should leave one side empty, hence the name "partial range".
let newStr = str[..<index]
The same stands for partial range from operators, just leave the other side empty:
let newStr = str[index...]
Keep in mind that these range operators return a Substring. If you want to convert it to a string, use String's initialization function:
let newStr = String(str[..<index])
You can read more about the new substrings here.
Convert Substring (Swift 3) to String Slicing (Swift 4)
Examples In Swift 3, 4:
let newStr = str.substring(to: index) // Swift 3
let newStr = String(str[..<index]) // Swift 4
let newStr = str.substring(from: index) // Swift 3
let newStr = String(str[index...]) // Swift 4
let range = firstIndex..<secondIndex // If you have a range
let newStr = = str.substring(with: range) // Swift 3
let newStr = String(str[range]) // Swift 4
Swift 5, 4
Usage
let text = "Hello world"
text[0] // H
text[...3] // "Hell"
text[6..<text.count] // world
text[NSRange(location: 6, length: 3)] // wor
Code
import Foundation
public extension String {
subscript(value: Int) -> Character {
self[index(at: value)]
}
}
public extension String {
subscript(value: NSRange) -> Substring {
self[value.lowerBound..<value.upperBound]
}
}
public extension String {
subscript(value: CountableClosedRange<Int>) -> Substring {
self[index(at: value.lowerBound)...index(at: value.upperBound)]
}
subscript(value: CountableRange<Int>) -> Substring {
self[index(at: value.lowerBound)..<index(at: value.upperBound)]
}
subscript(value: PartialRangeUpTo<Int>) -> Substring {
self[..<index(at: value.upperBound)]
}
subscript(value: PartialRangeThrough<Int>) -> Substring {
self[...index(at: value.upperBound)]
}
subscript(value: PartialRangeFrom<Int>) -> Substring {
self[index(at: value.lowerBound)...]
}
}
private extension String {
func index(at offset: Int) -> String.Index {
index(startIndex, offsetBy: offset)
}
}
Shorter in Swift 4/5:
let string = "123456"
let firstThree = String(string.prefix(3)) //"123"
let lastThree = String(string.suffix(3)) //"456"
Swift5
(Java's substring method):
extension String {
func subString(from: Int, to: Int) -> String {
let startIndex = self.index(self.startIndex, offsetBy: from)
let endIndex = self.index(self.startIndex, offsetBy: to)
return String(self[startIndex..<endIndex])
}
}
Usage:
var str = "Hello, Nick Michaels"
print(str.subString(from:7,to:20))
// print Nick Michaels
The conversion of your code to Swift 4 can also be done this way:
let str = "Hello, playground"
let index = str.index(of: ",")!
let substr = str.prefix(upTo: index)
You can use the code below to have a new string:
let newString = String(str.prefix(upTo: index))
substring(from: index) Converted to [index...]
Check the sample
let text = "1234567890"
let index = text.index(text.startIndex, offsetBy: 3)
text.substring(from: index) // "4567890" [Swift 3]
String(text[index...]) // "4567890" [Swift 4]
Some useful extensions:
extension String {
func substring(from: Int, to: Int) -> String {
let start = index(startIndex, offsetBy: from)
let end = index(start, offsetBy: to - from)
return String(self[start ..< end])
}
func substring(range: NSRange) -> String {
return substring(from: range.lowerBound, to: range.upperBound)
}
}
Example of uppercasedFirstCharacter convenience property in Swift3 and Swift4.
Property uppercasedFirstCharacterNew demonstrates how to use String slicing subscript in Swift4.
extension String {
public var uppercasedFirstCharacterOld: String {
if characters.count > 0 {
let splitIndex = index(after: startIndex)
let firstCharacter = substring(to: splitIndex).uppercased()
let sentence = substring(from: splitIndex)
return firstCharacter + sentence
} else {
return self
}
}
public var uppercasedFirstCharacterNew: String {
if characters.count > 0 {
let splitIndex = index(after: startIndex)
let firstCharacter = self[..<splitIndex].uppercased()
let sentence = self[splitIndex...]
return firstCharacter + sentence
} else {
return self
}
}
}
let lorem = "lorem".uppercasedFirstCharacterOld
print(lorem) // Prints "Lorem"
let ipsum = "ipsum".uppercasedFirstCharacterNew
print(ipsum) // Prints "Ipsum"
You can create your custom subString method using extension to class String as below:
extension String {
func subString(startIndex: Int, endIndex: Int) -> String {
let end = (endIndex - self.count) + 1
let indexStartOfText = self.index(self.startIndex, offsetBy: startIndex)
let indexEndOfText = self.index(self.endIndex, offsetBy: end)
let substring = self[indexStartOfText..<indexEndOfText]
return String(substring)
}
}
Creating SubString (prefix and suffix) from String using Swift 4:
let str : String = "ilike"
for i in 0...str.count {
let index = str.index(str.startIndex, offsetBy: i) // String.Index
let prefix = str[..<index] // String.SubSequence
let suffix = str[index...] // String.SubSequence
print("prefix \(prefix), suffix : \(suffix)")
}
Output
prefix , suffix : ilike
prefix i, suffix : like
prefix il, suffix : ike
prefix ili, suffix : ke
prefix ilik, suffix : e
prefix ilike, suffix :
If you want to generate a substring between 2 indices , use :
let substring1 = string[startIndex...endIndex] // including endIndex
let subString2 = string[startIndex..<endIndex] // excluding endIndex
I have written a string extension for replacement of 'String: subString:'
extension String {
func sliceByCharacter(from: Character, to: Character) -> String? {
let fromIndex = self.index(self.index(of: from)!, offsetBy: 1)
let toIndex = self.index(self.index(of: to)!, offsetBy: -1)
return String(self[fromIndex...toIndex])
}
func sliceByString(from:String, to:String) -> String? {
//From - startIndex
var range = self.range(of: from)
let subString = String(self[range!.upperBound...])
//To - endIndex
range = subString.range(of: to)
return String(subString[..<range!.lowerBound])
}
}
Usage : "Date(1511508780012+0530)".sliceByString(from: "(", to: "+")
Example Result : "1511508780012"
PS: Optionals are forced to unwrap. Please add Type safety check wherever necessary.
If you are trying to just get a substring up to a specific character, you don't need to find the index first, you can just use the prefix(while:) method
let str = "Hello, playground"
let subString = str.prefix { $0 != "," } // "Hello" as a String.SubSequence
When programming I often have strings with just plain A-Za-z and 0-9. No need for difficult Index actions. This extension is based on the plain old left / mid / right functions.
extension String {
// LEFT
// Returns the specified number of chars from the left of the string
// let str = "Hello"
// print(str.left(3)) // Hel
func left(_ to: Int) -> String {
return "\(self[..<self.index(startIndex, offsetBy: to)])"
}
// RIGHT
// Returns the specified number of chars from the right of the string
// let str = "Hello"
// print(str.left(3)) // llo
func right(_ from: Int) -> String {
return "\(self[self.index(startIndex, offsetBy: self.length-from)...])"
}
// MID
// Returns the specified number of chars from the startpoint of the string
// let str = "Hello"
// print(str.left(2,amount: 2)) // ll
func mid(_ from: Int, amount: Int) -> String {
let x = "\(self[self.index(startIndex, offsetBy: from)...])"
return x.left(amount)
}
}
Hope this will help little more :-
var string = "123456789"
If you want a substring after some particular index.
var indexStart = string.index(after: string.startIndex )// you can use any index in place of startIndex
var strIndexStart = String (string[indexStart...])//23456789
If you want a substring after removing some string at the end.
var indexEnd = string.index(before: string.endIndex)
var strIndexEnd = String (string[..<indexEnd])//12345678
you can also create indexes with the following code :-
var indexWithOffset = string.index(string.startIndex, offsetBy: 4)
with this method you can get specific range of string.you need to pass start index and after that total number of characters you want.
extension String{
func substring(fromIndex : Int,count : Int) -> String{
let startIndex = self.index(self.startIndex, offsetBy: fromIndex)
let endIndex = self.index(self.startIndex, offsetBy: fromIndex + count)
let range = startIndex..<endIndex
return String(self[range])
}
}
This is my solution, no warning, no errors, but perfect
let redStr: String = String(trimmStr[String.Index.init(encodedOffset: 0)..<String.Index.init(encodedOffset: 2)])
let greenStr: String = String(trimmStr[String.Index.init(encodedOffset: 3)..<String.Index.init(encodedOffset: 4)])
let blueStr: String = String(trimmStr[String.Index.init(encodedOffset: 5)..<String.Index.init(encodedOffset: 6)])
var str = "Hello, playground"
let indexcut = str.firstIndex(of: ",")
print(String(str[..<indexcut!]))
print(String(str[indexcut!...]))
You can try in this way and will get proper results.
the simples way that I use is :
String(Array(str)[2...4])
Swift 4, 5, 5+
Substring from Last
let str = "Hello World"
let removeFirstSix = String(str.dropFirst(6))
print(removeFirstSix) //World
Substring from First
let removeLastSix = String(str.dropLast(6))
print(removeLastSix) //Hello
Hope it would be helpful.
extension String {
func getSubString(_ char: Character) -> String {
var subString = ""
for eachChar in self {
if eachChar == char {
return subString
} else {
subString += String(eachChar)
}
}
return subString
}
}
let str: String = "Hello, playground"
print(str.getSubString(","))

Swift 4.0 Right Padding a String [duplicate]

I'm trying to print a list of Strings all padded to the same width.
In C, I would use something like printf("%40s", cstr), where cstr is a C string.
In Swift, the best I could come up is this:
line += String(format: "%40s",string.cStringUsingEncoding(<someEncoding>))
Is there a better way ?
In Swift 3 you can use:
let str = "Test string"
let paddedStr = str.padding(toLength: 20, withPad: " ", startingAt: 0)
Result string: "Test string "
If you need to pad to the left the text (right justify), you can write the following function as an extension to String:
extension String {
func leftPadding(toLength: Int, withPad character: Character) -> String {
let newLength = self.characters.count
if newLength < toLength {
return String(repeatElement(character, count: toLength - newLength)) + self
} else {
return self.substring(from: index(self.startIndex, offsetBy: newLength - toLength))
}
}
}
So if you write:
let str = "Test string"
let paddedStr = str.leftPadding(toLength: 20, withPad: " ")
Result string: " Test string"
In Swift 4.1 the substring method is deprecated and there are a number of new methods to obtain a substring. Either prefix, suffix or subscripting the String with a Range<String.Index>.
For the previous extension we can use the suffix method to accomplish the same result. Since the suffix method returns a String.SubSequence, it needs to be converted into a String before being returned.
extension String {
func leftPadding(toLength: Int, withPad character: Character) -> String {
let stringLength = self.count
if stringLength < toLength {
return String(repeatElement(character, count: toLength - stringLength)) + self
} else {
return String(self.suffix(toLength))
}
}
}
For Swift >= 3
line += string.padding(toLength: 40, withPad: " ", startingAt: 0)
For Swift < 3
NSString has the stringByPaddingToLength: method:
line += string.stringByPaddingToLength(40, withString: " ", startingAtIndex: 0)
extension RangeReplaceableCollection where Self: StringProtocol {
func paddingToLeft(upTo length: Int, using element: Element = " ") -> SubSequence {
return repeatElement(element, count: Swift.max(0, length-count)) + suffix(Swift.max(count, count-length))
}
}
"123".paddingToLeft(upTo: 5) // " 123"
"123".paddingToLeft(upTo: 5, using: "0") // "00123"
"123".paddingToLeft(upTo: 3, using: "0") // "123"
"$199.99".dropLast(3).paddingToLeft(upTo: 10, using: "_") // "______$199"
To replicate the same behaviour as padding(toLength:, withPad:, startingAt:) we can add rotateTo left functionality to RangeReplaceableCollection
extension RangeReplaceableCollection {
func rotatingLeft(positions: Int) -> SubSequence {
let index = self.index(startIndex, offsetBy: positions, limitedBy: endIndex) ?? endIndex
return self[index...] + self[..<index]
}
}
And implement it as follow:
extension RangeReplaceableCollection where Self: StringProtocol {
func paddingToLeft<S: StringProtocol & RangeReplaceableCollection>(upTo length: Int, with string: S, startingAt index: Int = 0) -> SubSequence {
let string = string.rotatingLeft(positions: index)
return repeatElement(string, count: length-count/string.count)
.joined().prefix(length-count) + suffix(Swift.max(count, count-length))
}
}
"123".paddingToLeft(upTo: 10, with: "abc", startingAt: 2) // "cabcabc123"
"123".padding(toLength: 10, withPad: "abc", startingAt: 2) // "123cabcabc"
Put all string-format-code into extension and reuse it wherever you want.
extension String {
func padding(length: Int) -> String {
return self.stringByPaddingToLength(length, withString: " ", startingAtIndex: 0)
}
func padding(length: Int, paddingString: String) -> String {
return self.stringByPaddingToLength(length, withString: paddingString, startingAtIndex: 0)
}
}
var str = "str"
print(str.padding(10)) // "str "
print(str.padding(10, paddingString: "+")) // "str+++++++"
The following two functions return a string padded to the given width, either left or right justified. It is pure Swift 4, no NSString, and no C string either. You may choose if a string longer than the padding width will be truncated or not.
extension String {
func rightJustified(width: Int, truncate: Bool = false) -> String {
guard width > count else {
return truncate ? String(suffix(width)) : self
}
return String(repeating: " ", count: width - count) + self
}
func leftJustified(width: Int, truncate: Bool = false) -> String {
guard width > count else {
return truncate ? String(prefix(width)) : self
}
return self + String(repeating: " ", count: width - count)
}
}
For left padding, you can use double-reverse trick:
String(String(s.reversed()).padding(toLength: 5, withPad: "0", startingAt: 0).reversed())
Of course, you can wrap it as an extension:
extension String {
func leftPadding(toLength: Int, withPad: String) -> String {
String(String(reversed()).padding(toLength: toLength, withPad: withPad, startingAt: 0).reversed())
}
}
Here's my solution, specific to String, but I'm sure someone smarter than I could make it more generic.
extension String {
func frontPadding(toLength length: Int, withPad pad: String, startingAt index: Int) -> String {
return String(String(self.reversed()).padding(toLength: length, withPad: pad, startingAt: index).reversed())
}
}
Swift 5:
I just spent an embarrassing amount of time playing with this same problem.
let short = "ab"
String(repeating: "0", count: 8 - short.count).appending(short)
// 000000ab
import Foundation // for NSString.padding()
/**
* Custom Extension's API
* ------------------------------
* • str.padEnd(_:_:)
* • str.padStart(_:_:)
* ------------------------------
* • int.padStart(_:_:forceSign:)
*/
extension String {
// str.padEnd(8, "-")
func padEnd(_ length: Int, _ pad: String) -> String {
return padding(toLength: length, withPad: pad, startingAt: 0)
}
// str.padStart(8, "*")
func padStart(_ length: Int, _ pad: String) -> String {
let str = String(self.reversed())
return String(str.padEnd(length, pad).reversed())
}
}
extension Int {
// int.padStart(8)
func padStart(
_ length: Int, // total length
_ pad: String = "0", // pad character
forceSign: Bool = false // force + sign if positive
) -> String {
let isNegative = self < 0
let n = abs(self)
let str = String(n).padStart(length, pad)
return
isNegative ? "- " + str :
forceSign ? "+ " + str :
str
}
}
// test run
let s1 = "abc"
[
s1.padEnd(15, "*"), // abc************
s1.padStart(15, "*"), // ************abc
3.padStart(8, forceSign: true), // + 00000003
(-125).padStart(8) // - 00000125
].forEach{ print($0) }
left padding is here
extension String {
func leftPadding(toLength: Int, withPad character: Character) -> String {
if count < toLength {
return String(repeating: character, count: toLength - count) + self
} else {
return self
}
}
}
result
"234".leftPadding(toLength: 1, withPad: "0") // 234
"234".leftPadding(toLength: 2, withPad: "0") // 234
"234".leftPadding(toLength: 3, withPad: "0") // 234
"234".leftPadding(toLength: 4, withPad: "0") // 0234
"234".leftPadding(toLength: 5, withPad: "0") // 00234

How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

I have the following simple code written in Swift 3:
let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)
From Xcode 9 beta 5, I get the following warning:
'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.
How can this slicing subscript with partial range from be used in Swift 4?
You should leave one side empty, hence the name "partial range".
let newStr = str[..<index]
The same stands for partial range from operators, just leave the other side empty:
let newStr = str[index...]
Keep in mind that these range operators return a Substring. If you want to convert it to a string, use String's initialization function:
let newStr = String(str[..<index])
You can read more about the new substrings here.
Convert Substring (Swift 3) to String Slicing (Swift 4)
Examples In Swift 3, 4:
let newStr = str.substring(to: index) // Swift 3
let newStr = String(str[..<index]) // Swift 4
let newStr = str.substring(from: index) // Swift 3
let newStr = String(str[index...]) // Swift 4
let range = firstIndex..<secondIndex // If you have a range
let newStr = = str.substring(with: range) // Swift 3
let newStr = String(str[range]) // Swift 4
Swift 5, 4
Usage
let text = "Hello world"
text[0] // H
text[...3] // "Hell"
text[6..<text.count] // world
text[NSRange(location: 6, length: 3)] // wor
Code
import Foundation
public extension String {
subscript(value: Int) -> Character {
self[index(at: value)]
}
}
public extension String {
subscript(value: NSRange) -> Substring {
self[value.lowerBound..<value.upperBound]
}
}
public extension String {
subscript(value: CountableClosedRange<Int>) -> Substring {
self[index(at: value.lowerBound)...index(at: value.upperBound)]
}
subscript(value: CountableRange<Int>) -> Substring {
self[index(at: value.lowerBound)..<index(at: value.upperBound)]
}
subscript(value: PartialRangeUpTo<Int>) -> Substring {
self[..<index(at: value.upperBound)]
}
subscript(value: PartialRangeThrough<Int>) -> Substring {
self[...index(at: value.upperBound)]
}
subscript(value: PartialRangeFrom<Int>) -> Substring {
self[index(at: value.lowerBound)...]
}
}
private extension String {
func index(at offset: Int) -> String.Index {
index(startIndex, offsetBy: offset)
}
}
Shorter in Swift 4/5:
let string = "123456"
let firstThree = String(string.prefix(3)) //"123"
let lastThree = String(string.suffix(3)) //"456"
Swift5
(Java's substring method):
extension String {
func subString(from: Int, to: Int) -> String {
let startIndex = self.index(self.startIndex, offsetBy: from)
let endIndex = self.index(self.startIndex, offsetBy: to)
return String(self[startIndex..<endIndex])
}
}
Usage:
var str = "Hello, Nick Michaels"
print(str.subString(from:7,to:20))
// print Nick Michaels
The conversion of your code to Swift 4 can also be done this way:
let str = "Hello, playground"
let index = str.index(of: ",")!
let substr = str.prefix(upTo: index)
You can use the code below to have a new string:
let newString = String(str.prefix(upTo: index))
substring(from: index) Converted to [index...]
Check the sample
let text = "1234567890"
let index = text.index(text.startIndex, offsetBy: 3)
text.substring(from: index) // "4567890" [Swift 3]
String(text[index...]) // "4567890" [Swift 4]
Some useful extensions:
extension String {
func substring(from: Int, to: Int) -> String {
let start = index(startIndex, offsetBy: from)
let end = index(start, offsetBy: to - from)
return String(self[start ..< end])
}
func substring(range: NSRange) -> String {
return substring(from: range.lowerBound, to: range.upperBound)
}
}
Example of uppercasedFirstCharacter convenience property in Swift3 and Swift4.
Property uppercasedFirstCharacterNew demonstrates how to use String slicing subscript in Swift4.
extension String {
public var uppercasedFirstCharacterOld: String {
if characters.count > 0 {
let splitIndex = index(after: startIndex)
let firstCharacter = substring(to: splitIndex).uppercased()
let sentence = substring(from: splitIndex)
return firstCharacter + sentence
} else {
return self
}
}
public var uppercasedFirstCharacterNew: String {
if characters.count > 0 {
let splitIndex = index(after: startIndex)
let firstCharacter = self[..<splitIndex].uppercased()
let sentence = self[splitIndex...]
return firstCharacter + sentence
} else {
return self
}
}
}
let lorem = "lorem".uppercasedFirstCharacterOld
print(lorem) // Prints "Lorem"
let ipsum = "ipsum".uppercasedFirstCharacterNew
print(ipsum) // Prints "Ipsum"
You can create your custom subString method using extension to class String as below:
extension String {
func subString(startIndex: Int, endIndex: Int) -> String {
let end = (endIndex - self.count) + 1
let indexStartOfText = self.index(self.startIndex, offsetBy: startIndex)
let indexEndOfText = self.index(self.endIndex, offsetBy: end)
let substring = self[indexStartOfText..<indexEndOfText]
return String(substring)
}
}
Creating SubString (prefix and suffix) from String using Swift 4:
let str : String = "ilike"
for i in 0...str.count {
let index = str.index(str.startIndex, offsetBy: i) // String.Index
let prefix = str[..<index] // String.SubSequence
let suffix = str[index...] // String.SubSequence
print("prefix \(prefix), suffix : \(suffix)")
}
Output
prefix , suffix : ilike
prefix i, suffix : like
prefix il, suffix : ike
prefix ili, suffix : ke
prefix ilik, suffix : e
prefix ilike, suffix :
If you want to generate a substring between 2 indices , use :
let substring1 = string[startIndex...endIndex] // including endIndex
let subString2 = string[startIndex..<endIndex] // excluding endIndex
I have written a string extension for replacement of 'String: subString:'
extension String {
func sliceByCharacter(from: Character, to: Character) -> String? {
let fromIndex = self.index(self.index(of: from)!, offsetBy: 1)
let toIndex = self.index(self.index(of: to)!, offsetBy: -1)
return String(self[fromIndex...toIndex])
}
func sliceByString(from:String, to:String) -> String? {
//From - startIndex
var range = self.range(of: from)
let subString = String(self[range!.upperBound...])
//To - endIndex
range = subString.range(of: to)
return String(subString[..<range!.lowerBound])
}
}
Usage : "Date(1511508780012+0530)".sliceByString(from: "(", to: "+")
Example Result : "1511508780012"
PS: Optionals are forced to unwrap. Please add Type safety check wherever necessary.
If you are trying to just get a substring up to a specific character, you don't need to find the index first, you can just use the prefix(while:) method
let str = "Hello, playground"
let subString = str.prefix { $0 != "," } // "Hello" as a String.SubSequence
When programming I often have strings with just plain A-Za-z and 0-9. No need for difficult Index actions. This extension is based on the plain old left / mid / right functions.
extension String {
// LEFT
// Returns the specified number of chars from the left of the string
// let str = "Hello"
// print(str.left(3)) // Hel
func left(_ to: Int) -> String {
return "\(self[..<self.index(startIndex, offsetBy: to)])"
}
// RIGHT
// Returns the specified number of chars from the right of the string
// let str = "Hello"
// print(str.left(3)) // llo
func right(_ from: Int) -> String {
return "\(self[self.index(startIndex, offsetBy: self.length-from)...])"
}
// MID
// Returns the specified number of chars from the startpoint of the string
// let str = "Hello"
// print(str.left(2,amount: 2)) // ll
func mid(_ from: Int, amount: Int) -> String {
let x = "\(self[self.index(startIndex, offsetBy: from)...])"
return x.left(amount)
}
}
Hope this will help little more :-
var string = "123456789"
If you want a substring after some particular index.
var indexStart = string.index(after: string.startIndex )// you can use any index in place of startIndex
var strIndexStart = String (string[indexStart...])//23456789
If you want a substring after removing some string at the end.
var indexEnd = string.index(before: string.endIndex)
var strIndexEnd = String (string[..<indexEnd])//12345678
you can also create indexes with the following code :-
var indexWithOffset = string.index(string.startIndex, offsetBy: 4)
with this method you can get specific range of string.you need to pass start index and after that total number of characters you want.
extension String{
func substring(fromIndex : Int,count : Int) -> String{
let startIndex = self.index(self.startIndex, offsetBy: fromIndex)
let endIndex = self.index(self.startIndex, offsetBy: fromIndex + count)
let range = startIndex..<endIndex
return String(self[range])
}
}
This is my solution, no warning, no errors, but perfect
let redStr: String = String(trimmStr[String.Index.init(encodedOffset: 0)..<String.Index.init(encodedOffset: 2)])
let greenStr: String = String(trimmStr[String.Index.init(encodedOffset: 3)..<String.Index.init(encodedOffset: 4)])
let blueStr: String = String(trimmStr[String.Index.init(encodedOffset: 5)..<String.Index.init(encodedOffset: 6)])
var str = "Hello, playground"
let indexcut = str.firstIndex(of: ",")
print(String(str[..<indexcut!]))
print(String(str[indexcut!...]))
You can try in this way and will get proper results.
the simples way that I use is :
String(Array(str)[2...4])
Swift 4, 5, 5+
Substring from Last
let str = "Hello World"
let removeFirstSix = String(str.dropFirst(6))
print(removeFirstSix) //World
Substring from First
let removeLastSix = String(str.dropLast(6))
print(removeLastSix) //Hello
Hope it would be helpful.
extension String {
func getSubString(_ char: Character) -> String {
var subString = ""
for eachChar in self {
if eachChar == char {
return subString
} else {
subString += String(eachChar)
}
}
return subString
}
}
let str: String = "Hello, playground"
print(str.getSubString(","))

Padding a swift String for printing

I'm trying to print a list of Strings all padded to the same width.
In C, I would use something like printf("%40s", cstr), where cstr is a C string.
In Swift, the best I could come up is this:
line += String(format: "%40s",string.cStringUsingEncoding(<someEncoding>))
Is there a better way ?
In Swift 3 you can use:
let str = "Test string"
let paddedStr = str.padding(toLength: 20, withPad: " ", startingAt: 0)
Result string: "Test string "
If you need to pad to the left the text (right justify), you can write the following function as an extension to String:
extension String {
func leftPadding(toLength: Int, withPad character: Character) -> String {
let newLength = self.characters.count
if newLength < toLength {
return String(repeatElement(character, count: toLength - newLength)) + self
} else {
return self.substring(from: index(self.startIndex, offsetBy: newLength - toLength))
}
}
}
So if you write:
let str = "Test string"
let paddedStr = str.leftPadding(toLength: 20, withPad: " ")
Result string: " Test string"
In Swift 4.1 the substring method is deprecated and there are a number of new methods to obtain a substring. Either prefix, suffix or subscripting the String with a Range<String.Index>.
For the previous extension we can use the suffix method to accomplish the same result. Since the suffix method returns a String.SubSequence, it needs to be converted into a String before being returned.
extension String {
func leftPadding(toLength: Int, withPad character: Character) -> String {
let stringLength = self.count
if stringLength < toLength {
return String(repeatElement(character, count: toLength - stringLength)) + self
} else {
return String(self.suffix(toLength))
}
}
}
For Swift >= 3
line += string.padding(toLength: 40, withPad: " ", startingAt: 0)
For Swift < 3
NSString has the stringByPaddingToLength: method:
line += string.stringByPaddingToLength(40, withString: " ", startingAtIndex: 0)
extension RangeReplaceableCollection where Self: StringProtocol {
func paddingToLeft(upTo length: Int, using element: Element = " ") -> SubSequence {
return repeatElement(element, count: Swift.max(0, length-count)) + suffix(Swift.max(count, count-length))
}
}
"123".paddingToLeft(upTo: 5) // " 123"
"123".paddingToLeft(upTo: 5, using: "0") // "00123"
"123".paddingToLeft(upTo: 3, using: "0") // "123"
"$199.99".dropLast(3).paddingToLeft(upTo: 10, using: "_") // "______$199"
To replicate the same behaviour as padding(toLength:, withPad:, startingAt:) we can add rotateTo left functionality to RangeReplaceableCollection
extension RangeReplaceableCollection {
func rotatingLeft(positions: Int) -> SubSequence {
let index = self.index(startIndex, offsetBy: positions, limitedBy: endIndex) ?? endIndex
return self[index...] + self[..<index]
}
}
And implement it as follow:
extension RangeReplaceableCollection where Self: StringProtocol {
func paddingToLeft<S: StringProtocol & RangeReplaceableCollection>(upTo length: Int, with string: S, startingAt index: Int = 0) -> SubSequence {
let string = string.rotatingLeft(positions: index)
return repeatElement(string, count: length-count/string.count)
.joined().prefix(length-count) + suffix(Swift.max(count, count-length))
}
}
"123".paddingToLeft(upTo: 10, with: "abc", startingAt: 2) // "cabcabc123"
"123".padding(toLength: 10, withPad: "abc", startingAt: 2) // "123cabcabc"
Put all string-format-code into extension and reuse it wherever you want.
extension String {
func padding(length: Int) -> String {
return self.stringByPaddingToLength(length, withString: " ", startingAtIndex: 0)
}
func padding(length: Int, paddingString: String) -> String {
return self.stringByPaddingToLength(length, withString: paddingString, startingAtIndex: 0)
}
}
var str = "str"
print(str.padding(10)) // "str "
print(str.padding(10, paddingString: "+")) // "str+++++++"
The following two functions return a string padded to the given width, either left or right justified. It is pure Swift 4, no NSString, and no C string either. You may choose if a string longer than the padding width will be truncated or not.
extension String {
func rightJustified(width: Int, truncate: Bool = false) -> String {
guard width > count else {
return truncate ? String(suffix(width)) : self
}
return String(repeating: " ", count: width - count) + self
}
func leftJustified(width: Int, truncate: Bool = false) -> String {
guard width > count else {
return truncate ? String(prefix(width)) : self
}
return self + String(repeating: " ", count: width - count)
}
}
For left padding, you can use double-reverse trick:
String(String(s.reversed()).padding(toLength: 5, withPad: "0", startingAt: 0).reversed())
Of course, you can wrap it as an extension:
extension String {
func leftPadding(toLength: Int, withPad: String) -> String {
String(String(reversed()).padding(toLength: toLength, withPad: withPad, startingAt: 0).reversed())
}
}
Here's my solution, specific to String, but I'm sure someone smarter than I could make it more generic.
extension String {
func frontPadding(toLength length: Int, withPad pad: String, startingAt index: Int) -> String {
return String(String(self.reversed()).padding(toLength: length, withPad: pad, startingAt: index).reversed())
}
}
Swift 5:
I just spent an embarrassing amount of time playing with this same problem.
let short = "ab"
String(repeating: "0", count: 8 - short.count).appending(short)
// 000000ab
import Foundation // for NSString.padding()
/**
* Custom Extension's API
* ------------------------------
* • str.padEnd(_:_:)
* • str.padStart(_:_:)
* ------------------------------
* • int.padStart(_:_:forceSign:)
*/
extension String {
// str.padEnd(8, "-")
func padEnd(_ length: Int, _ pad: String) -> String {
return padding(toLength: length, withPad: pad, startingAt: 0)
}
// str.padStart(8, "*")
func padStart(_ length: Int, _ pad: String) -> String {
let str = String(self.reversed())
return String(str.padEnd(length, pad).reversed())
}
}
extension Int {
// int.padStart(8)
func padStart(
_ length: Int, // total length
_ pad: String = "0", // pad character
forceSign: Bool = false // force + sign if positive
) -> String {
let isNegative = self < 0
let n = abs(self)
let str = String(n).padStart(length, pad)
return
isNegative ? "- " + str :
forceSign ? "+ " + str :
str
}
}
// test run
let s1 = "abc"
[
s1.padEnd(15, "*"), // abc************
s1.padStart(15, "*"), // ************abc
3.padStart(8, forceSign: true), // + 00000003
(-125).padStart(8) // - 00000125
].forEach{ print($0) }
left padding is here
extension String {
func leftPadding(toLength: Int, withPad character: Character) -> String {
if count < toLength {
return String(repeating: character, count: toLength - count) + self
} else {
return self
}
}
}
result
"234".leftPadding(toLength: 1, withPad: "0") // 234
"234".leftPadding(toLength: 2, withPad: "0") // 234
"234".leftPadding(toLength: 3, withPad: "0") // 234
"234".leftPadding(toLength: 4, withPad: "0") // 0234
"234".leftPadding(toLength: 5, withPad: "0") // 00234