How to Remove the first characters that same as in another textbox : vb6 - textbox

i have 2 textboxs, text1 and text2
i want to remove the first characters in text1 that same as in text2
text1 = 1234
text2 = 12
Result
text1 = 34

If you meant to say that you have three textboxes and they have the values:
Text1.Text = "1234"
Text2.Text = "12"
and you want Text3 to contain the remaining text of Text1 that is not common in Text2: then this is what you need to do:
Text1.Text = "1234"
Text2.Text = "12"
Text3.Text = Mid(Text1.Text, InStr(Text1.Text, Text2.Text) + Len(Text2.Text))
'The value in Text3.Text will be "34"
Otherwise, if you want the Result to be stored into Text1 again, just assign the result to Text1 instead of Text3
Text1.Text = Mid(Text1.Text, InStr(Text1.Text, Text2.Text) + Len(Text2.Text))
'The value in Text1.Text will be replaced with "34"

Related

Swift. Get first name and last initial from a name string [duplicate]

Say I have a string here:
var fullName: String = "First Last"
I want to split the string base on white space and assign the values to their respective variables
var fullNameArr = // something like: fullName.explode(" ")
var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]
Also, sometimes users might not have a last name.
Just call componentsSeparatedByString method on your fullName
import Foundation
var fullName: String = "First Last"
let fullNameArr = fullName.componentsSeparatedByString(" ")
var firstName: String = fullNameArr[0]
var lastName: String = fullNameArr[1]
Update for Swift 3+
import Foundation
let fullName = "First Last"
let fullNameArr = fullName.components(separatedBy: " ")
let name = fullNameArr[0]
let surname = fullNameArr[1]
The Swift way is to use the global split function, like so:
var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil
with Swift 2
In Swift 2 the use of split becomes a bit more complicated due to the introduction of the internal CharacterView type. This means that String no longer adopts the SequenceType or CollectionType protocols and you must instead use the .characters property to access a CharacterView type representation of a String instance. (Note: CharacterView does adopt SequenceType and CollectionType protocols).
let fullName = "First Last"
let fullNameArr = fullName.characters.split{$0 == " "}.map(String.init)
// or simply:
// let fullNameArr = fullName.characters.split{" "}.map(String.init)
fullNameArr[0] // First
fullNameArr[1] // Last
The easiest method to do this is by using componentsSeparatedBy:
For Swift 2:
import Foundation
let fullName : String = "First Last";
let fullNameArr : [String] = fullName.componentsSeparatedByString(" ")
// And then to access the individual words:
var firstName : String = fullNameArr[0]
var lastName : String = fullNameArr[1]
For Swift 3:
import Foundation
let fullName : String = "First Last"
let fullNameArr : [String] = fullName.components(separatedBy: " ")
// And then to access the individual words:
var firstName : String = fullNameArr[0]
var lastName : String = fullNameArr[1]
Swift Dev. 4.0 (May 24, 2017)
A new function split in Swift 4 (Beta).
import Foundation
let sayHello = "Hello Swift 4 2017";
let result = sayHello.split(separator: " ")
print(result)
Output:
["Hello", "Swift", "4", "2017"]
Accessing values:
print(result[0]) // Hello
print(result[1]) // Swift
print(result[2]) // 4
print(result[3]) // 2017
Xcode 8.1 / Swift 3.0.1
Here is the way multiple delimiters with array.
import Foundation
let mathString: String = "12-37*2/5"
let numbers = mathString.components(separatedBy: ["-", "*", "/"])
print(numbers)
Output:
["12", "37", "2", "5"]
Update for Swift 5.2 and the simpliest way
let paragraph = "Bob hit a ball, the hit BALL flew far after it was hit. Hello! Hie, How r u?"
let words = paragraph.components(separatedBy: [",", " ", "!",".","?"])
This prints,
["Bob", "hit", "a", "ball", "", "the", "hit", "BALL", "flew", "far",
"after", "it", "was", "hit", "", "Hello", "", "Hie", "", "How", "r",
"u", ""]
However, if you want to filter out empty string,
let words = paragraph.components(separatedBy: [",", " ", "!",".","?"]).filter({!$0.isEmpty})
Output,
["Bob", "hit", "a", "ball", "the", "hit", "BALL", "flew", "far",
"after", "it", "was", "hit", "Hello", "Hie", "How", "r", "u"]
But make sure, Foundation is imported.
Swift 4 or later
If you just need to properly format a person name, you can use PersonNameComponentsFormatter.
The PersonNameComponentsFormatter class provides localized
representations of the components of a person’s name, as represented
by a PersonNameComponents object. Use this class to create localized
names when displaying person name information to the user.
// iOS (9.0 and later), macOS (10.11 and later), tvOS (9.0 and later), watchOS (2.0 and later)
let nameFormatter = PersonNameComponentsFormatter()
let name = "Mr. Steven Paul Jobs Jr."
// personNameComponents requires iOS (10.0 and later)
if let nameComps = nameFormatter.personNameComponents(from: name) {
nameComps.namePrefix // Mr.
nameComps.givenName // Steven
nameComps.middleName // Paul
nameComps.familyName // Jobs
nameComps.nameSuffix // Jr.
// It can also be configured to format your names
// Default (same as medium), short, long or abbreviated
nameFormatter.style = .default
nameFormatter.string(from: nameComps) // "Steven Jobs"
nameFormatter.style = .short
nameFormatter.string(from: nameComps) // "Steven"
nameFormatter.style = .long
nameFormatter.string(from: nameComps) // "Mr. Steven Paul Jobs jr."
nameFormatter.style = .abbreviated
nameFormatter.string(from: nameComps) // SJ
// It can also be use to return an attributed string using annotatedString method
nameFormatter.style = .long
nameFormatter.annotatedString(from: nameComps) // "Mr. Steven Paul Jobs jr."
}
edit/update:
Swift 5 or later
For just splitting a string by non letter characters we can use the new Character property isLetter:
let fullName = "First Last"
let components = fullName.split{ !$0.isLetter }
print(components) // "["First", "Last"]\n"
As an alternative to WMios's answer, you can also use componentsSeparatedByCharactersInSet, which can be handy in the case you have more separators (blank space, comma, etc.).
With your specific input:
let separators = NSCharacterSet(charactersInString: " ")
var fullName: String = "First Last";
var words = fullName.componentsSeparatedByCharactersInSet(separators)
// words contains ["First", "Last"]
Using multiple separators:
let separators = NSCharacterSet(charactersInString: " ,")
var fullName: String = "Last, First Middle";
var words = fullName.componentsSeparatedByCharactersInSet(separators)
// words contains ["Last", "First", "Middle"]
Swift 4
let words = "these words will be elements in an array".components(separatedBy: " ")
The whitespace issue
Generally, people reinvent this problem and bad solutions over and over. Is this a space? " " and what about "\n", "\t" or some unicode whitespace character that you've never seen, in no small part because it is invisible. While you can get away with
A weak solution
import Foundation
let pieces = "Mary had little lamb".componentsSeparatedByString(" ")
If you ever need to shake your grip on reality watch a WWDC video on strings or dates. In short, it is almost always better to allow Apple to solve this kind of mundane task.
Robust Solution: Use NSCharacterSet
The way to do this correctly, IMHO, is to use NSCharacterSet since as stated earlier your whitespace might not be what you expect and Apple has provided a whitespace character set. To explore the various provided character sets check out Apple's NSCharacterSet developer documentation and then, only then, augment or construct a new character set if it doesn't fit your needs.
NSCharacterSet whitespaces
Returns a character set containing the characters in Unicode General
Category Zs and CHARACTER TABULATION (U+0009).
let longerString: String = "This is a test of the character set splitting system"
let components = longerString.components(separatedBy: .whitespaces)
print(components)
In Swift 4.2 and Xcode 10
//This is your str
let str = "This is my String" //Here replace with your string
Option 1
let items = str.components(separatedBy: " ")//Here replase space with your value and the result is Array.
//Direct single line of code
//let items = "This is my String".components(separatedBy: " ")
let str1 = items[0]
let str2 = items[1]
let str3 = items[2]
let str4 = items[3]
//OutPut
print(items.count)
print(str1)
print(str2)
print(str3)
print(str4)
print(items.first!)
print(items.last!)
Option 2
let items = str.split(separator: " ")
let str1 = String(items.first!)
let str2 = String(items.last!)
//Output
print(items.count)
print(items)
print(str1)
print(str2)
Option 3
let arr = str.split {$0 == " "}
print(arr)
Option 4
let line = "BLANCHE: I don't want realism. I want magic!"
print(line.split(separator: " "))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
By Apple Documentation....
let line = "BLANCHE: I don't want realism. I want magic!"
print(line.split(separator: " "))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
print(line.split(separator: " ", maxSplits: 1))//This can split your string into 2 parts
// Prints "["BLANCHE:", " I don\'t want realism. I want magic!"]"
print(line.split(separator: " ", maxSplits: 2))//This can split your string into 3 parts
print(line.split(separator: " ", omittingEmptySubsequences: false))//array contains empty strings where spaces were repeated.
// Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
print(line.split(separator: " ", omittingEmptySubsequences: true))//array not contains empty strings where spaces were repeated.
print(line.split(separator: " ", maxSplits: 4, omittingEmptySubsequences: false))
print(line.split(separator: " ", maxSplits: 3, omittingEmptySubsequences: true))
Only the split is the correct answer, here are the difference for more than 2 spaces.
Swift 5
var temp = "Hello world ni hao"
let arr = temp.components(separatedBy: .whitespacesAndNewlines)
// ["Hello", "world", "", "", "", "", "ni", "hao"]
let arr2 = temp.components(separatedBy: " ")
// ["Hello", "world", "", "", "", "", "ni", "hao"]
let arr3 = temp.split(whereSeparator: {$0 == " "})
// ["Hello", "world", "ni", "hao"]
Swift 4 makes it much easier to split characters, just use the new split function for Strings.
Example:
let s = "hi, hello"
let a = s.split(separator: ",")
print(a)
Now you got an array with 'hi' and ' hello'.
Swift 3
let line = "AAA BBB\t CCC"
let fields = line.components(separatedBy: .whitespaces).filter {!$0.isEmpty}
Returns three strings AAA, BBB and CCC
Filters out empty fields
Handles multiple spaces and tabulation characters
If you want to handle new lines, then replace .whitespaces with .whitespacesAndNewlines
Swift 4, Xcode 10 and iOS 12 Update 100% working
let fullName = "First Last"
let fullNameArr = fullName.components(separatedBy: " ")
let firstName = fullNameArr[0] //First
let lastName = fullNameArr[1] //Last
See the Apple's documentation here for further information.
Xcode 8.0 / Swift 3
let fullName = "First Last"
var fullNameArr = fullName.components(separatedBy: " ")
var firstname = fullNameArr[0] // First
var lastname = fullNameArr[1] // Last
Long Way:
var fullName: String = "First Last"
fullName += " " // this will help to see the last word
var newElement = "" //Empty String
var fullNameArr = [String]() //Empty Array
for Character in fullName.characters {
if Character == " " {
fullNameArr.append(newElement)
newElement = ""
} else {
newElement += "\(Character)"
}
}
var firsName = fullNameArr[0] // First
var lastName = fullNameArr[1] // Last
Most of these answers assume the input contains a space - not whitespace, and a single space at that. If you can safely make that assumption, then the accepted answer (from bennett) is quite elegant and also the method I'll be going with when I can.
When we can't make that assumption, a more robust solution needs to cover the following siutations that most answers here don't consider:
tabs/newlines/spaces (whitespace), including recurring characters
leading/trailing whitespace
Apple/Linux (\n) and Windows (\r\n) newline characters
To cover these cases this solution uses regex to convert all whitespace (including recurring and Windows newline characters) to a single space, trims, then splits by a single space:
Swift 3:
let searchInput = " First \r\n \n \t\t\tMiddle Last "
let searchTerms = searchInput
.replacingOccurrences(
of: "\\s+",
with: " ",
options: .regularExpression
)
.trimmingCharacters(in: .whitespaces)
.components(separatedBy: " ")
// searchTerms == ["First", "Middle", "Last"]
I had a scenario where multiple control characters can be present in the string I want to split. Rather than maintain an array of these, I just let Apple handle that part.
The following works with Swift 3.0.1 on iOS 10:
let myArray = myString.components(separatedBy: .controlCharacters)
I found an Interesting case, that
method 1
var data:[String] = split( featureData ) { $0 == "\u{003B}" }
When I used this command to split some symbol from the data that loaded from server, it can split while test in simulator and sync with test device, but it won't split in publish app, and Ad Hoc
It take me a lot of time to track this error, It might cursed from some Swift Version, or some iOS Version or neither
It's not about the HTML code also, since I try to stringByRemovingPercentEncoding and it's still not work
addition 10/10/2015
in Swift 2.0 this method has been changed to
var data:[String] = featureData.split {$0 == "\u{003B}"}
method 2
var data:[String] = featureData.componentsSeparatedByString("\u{003B}")
When I used this command, it can split the same data that load from server correctly
Conclusion, I really suggest to use the method 2
string.componentsSeparatedByString("")
Steps to split a string into an array in Swift 4.
assign string
based on # splitting.
Note: variableName.components(separatedBy: "split keyword")
let fullName: String = "First Last # triggerd event of the session by session storage # it can be divided by the event of the trigger."
let fullNameArr = fullName.components(separatedBy: "#")
print("split", fullNameArr)
This gives an array of split parts directly
var fullNameArr = fullName.components(separatedBy:" ")
then you can use like this,
var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]
Or without closures you can do just this in Swift 2:
let fullName = "First Last"
let fullNameArr = fullName.characters.split(" ")
let firstName = String(fullNameArr[0])
Swift 4
let string = "loremipsum.dolorsant.amet:"
let result = string.components(separatedBy: ".")
print(result[0])
print(result[1])
print(result[2])
print("total: \(result.count)")
Output
loremipsum
dolorsant
amet:
total: 3
The simplest solution is
let fullName = "First Last"
let components = fullName.components(separatedBy: .whitespacesAndNewlines).compactMap { $0.isEmpty ? nil : $0 }
This will handled multiple white spaces in a row of different types (white space, tabs, newlines etc) and only returns a two element array, you can change the CharacterSet to include more character you like, if you want to get cleaver you can use Regular Expression Decoder, this lets you write regular expression that can be used to decoded string directly into your own class/struct that implement the Decoding protocol. For something like this is over kill, but if you are using it as an example for more complicate string it may make more sense.
Let's say you have a variable named "Hello World" and if you want to split it and store it into two different variables you can use like this:
var fullText = "Hello World"
let firstWord = fullText.text?.components(separatedBy: " ").first
let lastWord = fullText.text?.components(separatedBy: " ").last
This has Changed again in Beta 5. Weee! It's now a method on CollectionType
Old:
var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
New:
var fullName = "First Last"
var fullNameArr = fullName.split {$0 == " "}
Apples Release Notes
String handling is still a challenge in Swift and it keeps changing significantly, as you can see from other answers. Hopefully things settle down and it gets simpler. This is the way to do it with the current 3.0 version of Swift with multiple separator characters.
Swift 3:
let chars = CharacterSet(charactersIn: ".,; -")
let split = phrase.components(separatedBy: chars)
// Or if the enums do what you want, these are preferred.
let chars2 = CharacterSet.alphaNumerics // .whitespaces, .punctuation, .capitalizedLetters etc
let split2 = phrase.components(separatedBy: chars2)
I was looking for loosy split, such as PHP's explode where empty sequences are included in resulting array, this worked for me:
"First ".split(separator: " ", maxSplits: 1, omittingEmptySubsequences: false)
Output:
["First", ""]
let str = "one two"
let strSplit = str.characters.split(" ").map(String.init) // returns ["one", "two"]
Xcode 7.2 (7C68)
Swift 2.2
Error Handling & capitalizedString Added :
func setFullName(fullName: String) {
var fullNameComponents = fullName.componentsSeparatedByString(" ")
self.fname = fullNameComponents.count > 0 ? fullNameComponents[0]: ""
self.sname = fullNameComponents.count > 1 ? fullNameComponents[1]: ""
self.fname = self.fname!.capitalizedString
self.sname = self.sname!.capitalizedString
}
OFFTOP:
For people searching how to split a string with substring (not a character), then here is working solution:
// TESTING
let str1 = "Hello user! What user's details? Here user rounded with space."
let a = str1.split(withSubstring: "user") // <-------------- HERE IS A SPLIT
print(a) // ["Hello ", "! What ", "\'s details? Here ", " rounded with space."]
// testing the result
var result = ""
for item in a {
if !result.isEmpty {
result += "user"
}
result += item
}
print(str1) // "Hello user! What user's details? Here user rounded with space."
print(result) // "Hello user! What user's details? Here user rounded with space."
print(result == str1) // true
/// Extension providing `split` and `substring` methods.
extension String {
/// Split given string with substring into array
/// - Parameters:
/// - string: the string
/// - substring: the substring to search
/// - Returns: array of components
func split(withSubstring substring: String) -> [String] {
var a = [String]()
var str = self
while let range = str.range(of: substring) {
let i = str.distance(from: str.startIndex, to: range.lowerBound)
let j = str.distance(from: str.startIndex, to: range.upperBound)
let left = str.substring(index: 0, length: i)
let right = str.substring(index: j, length: str.length - j)
a.append(left)
str = right
}
if !str.isEmpty {
a.append(str)
}
return a
}
/// the length of the string
public var length: Int {
return self.count
}
/// Get substring, e.g. "ABCDE".substring(index: 2, length: 3) -> "CDE"
///
/// - parameter index: the start index
/// - parameter length: the length of the substring
///
/// - returns: the substring
public func substring(index: Int, length: Int) -> String {
if self.length <= index {
return ""
}
let leftIndex = self.index(self.startIndex, offsetBy: index)
if self.length <= index + length {
return String(self[leftIndex..<self.endIndex])
}
let rightIndex = self.index(self.endIndex, offsetBy: -(self.length - index - length))
return String(self[leftIndex..<rightIndex])
}
}

Swift separate String into array of data split at certain characters [duplicate]

Say I have a string here:
var fullName: String = "First Last"
I want to split the string base on white space and assign the values to their respective variables
var fullNameArr = // something like: fullName.explode(" ")
var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]
Also, sometimes users might not have a last name.
Just call componentsSeparatedByString method on your fullName
import Foundation
var fullName: String = "First Last"
let fullNameArr = fullName.componentsSeparatedByString(" ")
var firstName: String = fullNameArr[0]
var lastName: String = fullNameArr[1]
Update for Swift 3+
import Foundation
let fullName = "First Last"
let fullNameArr = fullName.components(separatedBy: " ")
let name = fullNameArr[0]
let surname = fullNameArr[1]
The Swift way is to use the global split function, like so:
var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil
with Swift 2
In Swift 2 the use of split becomes a bit more complicated due to the introduction of the internal CharacterView type. This means that String no longer adopts the SequenceType or CollectionType protocols and you must instead use the .characters property to access a CharacterView type representation of a String instance. (Note: CharacterView does adopt SequenceType and CollectionType protocols).
let fullName = "First Last"
let fullNameArr = fullName.characters.split{$0 == " "}.map(String.init)
// or simply:
// let fullNameArr = fullName.characters.split{" "}.map(String.init)
fullNameArr[0] // First
fullNameArr[1] // Last
The easiest method to do this is by using componentsSeparatedBy:
For Swift 2:
import Foundation
let fullName : String = "First Last";
let fullNameArr : [String] = fullName.componentsSeparatedByString(" ")
// And then to access the individual words:
var firstName : String = fullNameArr[0]
var lastName : String = fullNameArr[1]
For Swift 3:
import Foundation
let fullName : String = "First Last"
let fullNameArr : [String] = fullName.components(separatedBy: " ")
// And then to access the individual words:
var firstName : String = fullNameArr[0]
var lastName : String = fullNameArr[1]
Swift Dev. 4.0 (May 24, 2017)
A new function split in Swift 4 (Beta).
import Foundation
let sayHello = "Hello Swift 4 2017";
let result = sayHello.split(separator: " ")
print(result)
Output:
["Hello", "Swift", "4", "2017"]
Accessing values:
print(result[0]) // Hello
print(result[1]) // Swift
print(result[2]) // 4
print(result[3]) // 2017
Xcode 8.1 / Swift 3.0.1
Here is the way multiple delimiters with array.
import Foundation
let mathString: String = "12-37*2/5"
let numbers = mathString.components(separatedBy: ["-", "*", "/"])
print(numbers)
Output:
["12", "37", "2", "5"]
Update for Swift 5.2 and the simpliest way
let paragraph = "Bob hit a ball, the hit BALL flew far after it was hit. Hello! Hie, How r u?"
let words = paragraph.components(separatedBy: [",", " ", "!",".","?"])
This prints,
["Bob", "hit", "a", "ball", "", "the", "hit", "BALL", "flew", "far",
"after", "it", "was", "hit", "", "Hello", "", "Hie", "", "How", "r",
"u", ""]
However, if you want to filter out empty string,
let words = paragraph.components(separatedBy: [",", " ", "!",".","?"]).filter({!$0.isEmpty})
Output,
["Bob", "hit", "a", "ball", "the", "hit", "BALL", "flew", "far",
"after", "it", "was", "hit", "Hello", "Hie", "How", "r", "u"]
But make sure, Foundation is imported.
Swift 4 or later
If you just need to properly format a person name, you can use PersonNameComponentsFormatter.
The PersonNameComponentsFormatter class provides localized
representations of the components of a person’s name, as represented
by a PersonNameComponents object. Use this class to create localized
names when displaying person name information to the user.
// iOS (9.0 and later), macOS (10.11 and later), tvOS (9.0 and later), watchOS (2.0 and later)
let nameFormatter = PersonNameComponentsFormatter()
let name = "Mr. Steven Paul Jobs Jr."
// personNameComponents requires iOS (10.0 and later)
if let nameComps = nameFormatter.personNameComponents(from: name) {
nameComps.namePrefix // Mr.
nameComps.givenName // Steven
nameComps.middleName // Paul
nameComps.familyName // Jobs
nameComps.nameSuffix // Jr.
// It can also be configured to format your names
// Default (same as medium), short, long or abbreviated
nameFormatter.style = .default
nameFormatter.string(from: nameComps) // "Steven Jobs"
nameFormatter.style = .short
nameFormatter.string(from: nameComps) // "Steven"
nameFormatter.style = .long
nameFormatter.string(from: nameComps) // "Mr. Steven Paul Jobs jr."
nameFormatter.style = .abbreviated
nameFormatter.string(from: nameComps) // SJ
// It can also be use to return an attributed string using annotatedString method
nameFormatter.style = .long
nameFormatter.annotatedString(from: nameComps) // "Mr. Steven Paul Jobs jr."
}
edit/update:
Swift 5 or later
For just splitting a string by non letter characters we can use the new Character property isLetter:
let fullName = "First Last"
let components = fullName.split{ !$0.isLetter }
print(components) // "["First", "Last"]\n"
As an alternative to WMios's answer, you can also use componentsSeparatedByCharactersInSet, which can be handy in the case you have more separators (blank space, comma, etc.).
With your specific input:
let separators = NSCharacterSet(charactersInString: " ")
var fullName: String = "First Last";
var words = fullName.componentsSeparatedByCharactersInSet(separators)
// words contains ["First", "Last"]
Using multiple separators:
let separators = NSCharacterSet(charactersInString: " ,")
var fullName: String = "Last, First Middle";
var words = fullName.componentsSeparatedByCharactersInSet(separators)
// words contains ["Last", "First", "Middle"]
Swift 4
let words = "these words will be elements in an array".components(separatedBy: " ")
The whitespace issue
Generally, people reinvent this problem and bad solutions over and over. Is this a space? " " and what about "\n", "\t" or some unicode whitespace character that you've never seen, in no small part because it is invisible. While you can get away with
A weak solution
import Foundation
let pieces = "Mary had little lamb".componentsSeparatedByString(" ")
If you ever need to shake your grip on reality watch a WWDC video on strings or dates. In short, it is almost always better to allow Apple to solve this kind of mundane task.
Robust Solution: Use NSCharacterSet
The way to do this correctly, IMHO, is to use NSCharacterSet since as stated earlier your whitespace might not be what you expect and Apple has provided a whitespace character set. To explore the various provided character sets check out Apple's NSCharacterSet developer documentation and then, only then, augment or construct a new character set if it doesn't fit your needs.
NSCharacterSet whitespaces
Returns a character set containing the characters in Unicode General
Category Zs and CHARACTER TABULATION (U+0009).
let longerString: String = "This is a test of the character set splitting system"
let components = longerString.components(separatedBy: .whitespaces)
print(components)
In Swift 4.2 and Xcode 10
//This is your str
let str = "This is my String" //Here replace with your string
Option 1
let items = str.components(separatedBy: " ")//Here replase space with your value and the result is Array.
//Direct single line of code
//let items = "This is my String".components(separatedBy: " ")
let str1 = items[0]
let str2 = items[1]
let str3 = items[2]
let str4 = items[3]
//OutPut
print(items.count)
print(str1)
print(str2)
print(str3)
print(str4)
print(items.first!)
print(items.last!)
Option 2
let items = str.split(separator: " ")
let str1 = String(items.first!)
let str2 = String(items.last!)
//Output
print(items.count)
print(items)
print(str1)
print(str2)
Option 3
let arr = str.split {$0 == " "}
print(arr)
Option 4
let line = "BLANCHE: I don't want realism. I want magic!"
print(line.split(separator: " "))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
By Apple Documentation....
let line = "BLANCHE: I don't want realism. I want magic!"
print(line.split(separator: " "))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
print(line.split(separator: " ", maxSplits: 1))//This can split your string into 2 parts
// Prints "["BLANCHE:", " I don\'t want realism. I want magic!"]"
print(line.split(separator: " ", maxSplits: 2))//This can split your string into 3 parts
print(line.split(separator: " ", omittingEmptySubsequences: false))//array contains empty strings where spaces were repeated.
// Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
print(line.split(separator: " ", omittingEmptySubsequences: true))//array not contains empty strings where spaces were repeated.
print(line.split(separator: " ", maxSplits: 4, omittingEmptySubsequences: false))
print(line.split(separator: " ", maxSplits: 3, omittingEmptySubsequences: true))
Only the split is the correct answer, here are the difference for more than 2 spaces.
Swift 5
var temp = "Hello world ni hao"
let arr = temp.components(separatedBy: .whitespacesAndNewlines)
// ["Hello", "world", "", "", "", "", "ni", "hao"]
let arr2 = temp.components(separatedBy: " ")
// ["Hello", "world", "", "", "", "", "ni", "hao"]
let arr3 = temp.split(whereSeparator: {$0 == " "})
// ["Hello", "world", "ni", "hao"]
Swift 4 makes it much easier to split characters, just use the new split function for Strings.
Example:
let s = "hi, hello"
let a = s.split(separator: ",")
print(a)
Now you got an array with 'hi' and ' hello'.
Swift 3
let line = "AAA BBB\t CCC"
let fields = line.components(separatedBy: .whitespaces).filter {!$0.isEmpty}
Returns three strings AAA, BBB and CCC
Filters out empty fields
Handles multiple spaces and tabulation characters
If you want to handle new lines, then replace .whitespaces with .whitespacesAndNewlines
Swift 4, Xcode 10 and iOS 12 Update 100% working
let fullName = "First Last"
let fullNameArr = fullName.components(separatedBy: " ")
let firstName = fullNameArr[0] //First
let lastName = fullNameArr[1] //Last
See the Apple's documentation here for further information.
Xcode 8.0 / Swift 3
let fullName = "First Last"
var fullNameArr = fullName.components(separatedBy: " ")
var firstname = fullNameArr[0] // First
var lastname = fullNameArr[1] // Last
Long Way:
var fullName: String = "First Last"
fullName += " " // this will help to see the last word
var newElement = "" //Empty String
var fullNameArr = [String]() //Empty Array
for Character in fullName.characters {
if Character == " " {
fullNameArr.append(newElement)
newElement = ""
} else {
newElement += "\(Character)"
}
}
var firsName = fullNameArr[0] // First
var lastName = fullNameArr[1] // Last
Most of these answers assume the input contains a space - not whitespace, and a single space at that. If you can safely make that assumption, then the accepted answer (from bennett) is quite elegant and also the method I'll be going with when I can.
When we can't make that assumption, a more robust solution needs to cover the following siutations that most answers here don't consider:
tabs/newlines/spaces (whitespace), including recurring characters
leading/trailing whitespace
Apple/Linux (\n) and Windows (\r\n) newline characters
To cover these cases this solution uses regex to convert all whitespace (including recurring and Windows newline characters) to a single space, trims, then splits by a single space:
Swift 3:
let searchInput = " First \r\n \n \t\t\tMiddle Last "
let searchTerms = searchInput
.replacingOccurrences(
of: "\\s+",
with: " ",
options: .regularExpression
)
.trimmingCharacters(in: .whitespaces)
.components(separatedBy: " ")
// searchTerms == ["First", "Middle", "Last"]
I had a scenario where multiple control characters can be present in the string I want to split. Rather than maintain an array of these, I just let Apple handle that part.
The following works with Swift 3.0.1 on iOS 10:
let myArray = myString.components(separatedBy: .controlCharacters)
I found an Interesting case, that
method 1
var data:[String] = split( featureData ) { $0 == "\u{003B}" }
When I used this command to split some symbol from the data that loaded from server, it can split while test in simulator and sync with test device, but it won't split in publish app, and Ad Hoc
It take me a lot of time to track this error, It might cursed from some Swift Version, or some iOS Version or neither
It's not about the HTML code also, since I try to stringByRemovingPercentEncoding and it's still not work
addition 10/10/2015
in Swift 2.0 this method has been changed to
var data:[String] = featureData.split {$0 == "\u{003B}"}
method 2
var data:[String] = featureData.componentsSeparatedByString("\u{003B}")
When I used this command, it can split the same data that load from server correctly
Conclusion, I really suggest to use the method 2
string.componentsSeparatedByString("")
Steps to split a string into an array in Swift 4.
assign string
based on # splitting.
Note: variableName.components(separatedBy: "split keyword")
let fullName: String = "First Last # triggerd event of the session by session storage # it can be divided by the event of the trigger."
let fullNameArr = fullName.components(separatedBy: "#")
print("split", fullNameArr)
This gives an array of split parts directly
var fullNameArr = fullName.components(separatedBy:" ")
then you can use like this,
var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]
Or without closures you can do just this in Swift 2:
let fullName = "First Last"
let fullNameArr = fullName.characters.split(" ")
let firstName = String(fullNameArr[0])
Swift 4
let string = "loremipsum.dolorsant.amet:"
let result = string.components(separatedBy: ".")
print(result[0])
print(result[1])
print(result[2])
print("total: \(result.count)")
Output
loremipsum
dolorsant
amet:
total: 3
The simplest solution is
let fullName = "First Last"
let components = fullName.components(separatedBy: .whitespacesAndNewlines).compactMap { $0.isEmpty ? nil : $0 }
This will handled multiple white spaces in a row of different types (white space, tabs, newlines etc) and only returns a two element array, you can change the CharacterSet to include more character you like, if you want to get cleaver you can use Regular Expression Decoder, this lets you write regular expression that can be used to decoded string directly into your own class/struct that implement the Decoding protocol. For something like this is over kill, but if you are using it as an example for more complicate string it may make more sense.
Let's say you have a variable named "Hello World" and if you want to split it and store it into two different variables you can use like this:
var fullText = "Hello World"
let firstWord = fullText.text?.components(separatedBy: " ").first
let lastWord = fullText.text?.components(separatedBy: " ").last
This has Changed again in Beta 5. Weee! It's now a method on CollectionType
Old:
var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
New:
var fullName = "First Last"
var fullNameArr = fullName.split {$0 == " "}
Apples Release Notes
String handling is still a challenge in Swift and it keeps changing significantly, as you can see from other answers. Hopefully things settle down and it gets simpler. This is the way to do it with the current 3.0 version of Swift with multiple separator characters.
Swift 3:
let chars = CharacterSet(charactersIn: ".,; -")
let split = phrase.components(separatedBy: chars)
// Or if the enums do what you want, these are preferred.
let chars2 = CharacterSet.alphaNumerics // .whitespaces, .punctuation, .capitalizedLetters etc
let split2 = phrase.components(separatedBy: chars2)
I was looking for loosy split, such as PHP's explode where empty sequences are included in resulting array, this worked for me:
"First ".split(separator: " ", maxSplits: 1, omittingEmptySubsequences: false)
Output:
["First", ""]
let str = "one two"
let strSplit = str.characters.split(" ").map(String.init) // returns ["one", "two"]
Xcode 7.2 (7C68)
Swift 2.2
Error Handling & capitalizedString Added :
func setFullName(fullName: String) {
var fullNameComponents = fullName.componentsSeparatedByString(" ")
self.fname = fullNameComponents.count > 0 ? fullNameComponents[0]: ""
self.sname = fullNameComponents.count > 1 ? fullNameComponents[1]: ""
self.fname = self.fname!.capitalizedString
self.sname = self.sname!.capitalizedString
}
OFFTOP:
For people searching how to split a string with substring (not a character), then here is working solution:
// TESTING
let str1 = "Hello user! What user's details? Here user rounded with space."
let a = str1.split(withSubstring: "user") // <-------------- HERE IS A SPLIT
print(a) // ["Hello ", "! What ", "\'s details? Here ", " rounded with space."]
// testing the result
var result = ""
for item in a {
if !result.isEmpty {
result += "user"
}
result += item
}
print(str1) // "Hello user! What user's details? Here user rounded with space."
print(result) // "Hello user! What user's details? Here user rounded with space."
print(result == str1) // true
/// Extension providing `split` and `substring` methods.
extension String {
/// Split given string with substring into array
/// - Parameters:
/// - string: the string
/// - substring: the substring to search
/// - Returns: array of components
func split(withSubstring substring: String) -> [String] {
var a = [String]()
var str = self
while let range = str.range(of: substring) {
let i = str.distance(from: str.startIndex, to: range.lowerBound)
let j = str.distance(from: str.startIndex, to: range.upperBound)
let left = str.substring(index: 0, length: i)
let right = str.substring(index: j, length: str.length - j)
a.append(left)
str = right
}
if !str.isEmpty {
a.append(str)
}
return a
}
/// the length of the string
public var length: Int {
return self.count
}
/// Get substring, e.g. "ABCDE".substring(index: 2, length: 3) -> "CDE"
///
/// - parameter index: the start index
/// - parameter length: the length of the substring
///
/// - returns: the substring
public func substring(index: Int, length: Int) -> String {
if self.length <= index {
return ""
}
let leftIndex = self.index(self.startIndex, offsetBy: index)
if self.length <= index + length {
return String(self[leftIndex..<self.endIndex])
}
let rightIndex = self.index(self.endIndex, offsetBy: -(self.length - index - length))
return String(self[leftIndex..<rightIndex])
}
}

Avoiding formatting of DOORS object text using DXL

I am writing DXL script in which few object text has borders(like one complete row of table is copied).
I have to emphasize the "shall" word in shall.
But using findPlainText() method, it changes the formatting of object text which have borders.
Initially the objects before scripts run is:
After running the script to make "shall" word Bold, i wrote DXL script:
void Change_Shall(Object o, string objText)
{
int off=0
int len=0
string StartUpperText = ""
string FontText = ""
string StartText = ""
string FindText = ""
bool IsChanged = false
string OriginalObjText = objText
string UpperFontObjText = upper(objText)
while (findPlainText(UpperFontObjText, "SHALL", off, len, true, false))
{
StartUpperText = UpperFontObjText[0:off-1]
UpperFontObjText = UpperFontObjText[off+len:]
FindText = OriginalObjText[off:off+len-1]
StartText = OriginalObjText[0:off-1]
OriginalObjText = OriginalObjText[off+len:]
if(FontText == "")
FontText = StartText "{\\b " FindText "}"
else
FontText = FontText StartText "{\\b " FindText "}"
//print FindText "\t\t" UpperFontObjText "\n"
IsChanged = true
off = 0
len = 0
}
if(IsChanged == true)
o."Object Text" = richText FontText OriginalObjText
}
The object text with border after this script runs get changes like
How can formatting of object text with borders be avoided and border is preserved in the object text.
I'm not sure how you're getting "Object Text" from Object o, but my guess is you're using o."Object Text" "" to cast it as a string. Is that right?
If so, then that is stripping all the rich text (including your borders) before you do anything to it. Try using string objText = richTextWithOle o."Object Text", or string objText = richText o."Object Text", and then try removing the unnecessary parameter to your function string objText, as a reference to this already comes along with Object o
I'm assuming based on how your table looks that it is a RichText table, in which case I believe your code will still work, it's just that I suspect you're starting with a string stripped of richText and adding richtext to it. Sometimes Word OLE objects with tables can look like that too, in which case you'd have to use COM to manipulate the OLE.
Hope this helps.

Add string to beginning of another string

Basic question
I have 2 strings. I want to add one string to another? Here's an example:
var secondString= "is your name."
var firstString = "Mike, "
Here I have 2 strings. I want to add firstString to secondString, NOT vice versa. (Which would be: firstString += secondString.)
More detail
I have 5 string
let first = "7898"
let second = "00"
let third = "5481"
let fourth = "4782"
var fullString = "\(third):\(fourth)"
I know for sure that third and fourth will be in fullString, but I don't know about first and second.
So I will make an if statement checking if second has 00. If it does, first and second won't go in fullString. If it doesn't, second will go intofullString`.
Then I will check if first has 00. If it does, then first won't go inside of fullString, and if not, it will go.
The thing is, I need them in the same order: first, second, third fourth. So in the if statement, I need a way to potentially add first and second at the beginning of fullString.
Re. your basic question:
secondString = "\(firstString)\(secondString)"
or
secondString = firstString + secondString
Here is a way to insert string at the beginning "without resetting" per your comment (first at front of second):
let range = second.startIndex..<second.startIndex
second.replaceRange(range, with: first)
Re. your "more detail" question:
var fullString: String
if second == "00" {
fullString = third + fourth
} else if first == "00" {
fullString = second + third + fourth
} else {
fullString = first + second + third + fourth
}
From the Apple documentation:
String values can be added together (or concatenated) with the addition operator (+) to create a new String value:
let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2
// welcome now equals "hello there"
You can also append a String value to an existing String variable with the addition assignment operator (+=):
var instruction = "look over"
instruction += string2
// instruction now equals "look over there"
You can append a Character value to a String variable with the String type’s append() method:
let exclamationMark: Character = "!"
welcome.append(exclamationMark)
// welcome now equals "hello there!"
So you are pretty much free to add these in any way shape or form.
Which includes
secondstring += firststring
Edit to accommodate the new information:
Strings in Swift are mutable which means you can always add to a string in-place without recreating any objects.
Something like (pseudo-code)
if(second != "00")
{
fullstring = second + fullstring
//only do something with first if second != 00
if(first != "00")
{
fullstring = first + fullstring
}
}

Swift - Remove " character from string

I have a string which is "Optional("5")". I need to remove the "" surrounding the 5. I have removed the 'Optional' by doing:
text2 = text2.stringByReplacingOccurrencesOfString("Optional(", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
I am having difficulties removing the " characters as they designate the end of a string in the code.
Swift uses backslash to escape double quotes. Here is the list of escaped special characters in Swift:
\0 (null character)
\\ (backslash)
\t (horizontal tab)
\n (line feed)
\r (carriage return)
\" (double quote)
\' (single quote)
This should work:
text2 = text2.replacingOccurrences(of: "\\", with: "", options: NSString.CompareOptions.literal, range: nil)
Swift 3 and Swift 4:
text2 = text2.textureName.replacingOccurrences(of: "\"", with: "", options: NSString.CompareOptions.literal, range:nil)
Latest documents updated to Swift 3.0.1 have:
Null Character (\0)
Backslash (\\)
Horizontal Tab (\t)
Line Feed (\n)
Carriage Return (\r)
Double Quote (\")
Single Quote (\')
Unicode scalar (\u{n}), where n is between one and eight hexadecimal digits
If you need more details you can take a look to the official docs here
Here is the swift 3 updated answer
var editedText = myLabel.text?.replacingOccurrences(of: "\"", with: "")
Null Character (\0)
Backslash (\\)
Horizontal Tab (\t)
Line Feed (\n)
Carriage Return (\r)
Double Quote (\")
Single Quote (\')
Unicode scalar (\u{n})
To remove the optional you only should do this
println("\(text2!)")
cause if you dont use "!" it takes the optional value of text2
And to remove "" from 5 you have to convert it to NSInteger or NSNumber easy peasy. It has "" cause its an string.
Replacing for Removing is not quite logical.
String.filter allows to iterate a string char by char and keep only true assertion.
Swift 4 & 5
var aString = "Optional(\"5\")"
aString = aString.filter { $0 != "\"" }
> Optional(5)
Or to extend
var aString = "Optional(\"5\")"
let filteredChars = "\"\n\t"
aString = aString.filter { filteredChars.range(of: String($0)) == nil }
> Optional(5)
I've eventually got this to work in the playground, having multiple characters I'm trying to remove from a string:
var otherstring = "lat\" : 40.7127837,\n"
var new = otherstring.stringByTrimmingCharactersInSet(NSCharacterSet.init(charactersInString: "la t, \n \" ':"))
count(new) //result = 10
println(new)
//yielding what I'm after just the numeric portion 40.7127837
If you want to remove more characters for example "a", "A", "b", "B", "c", "C" from string you can do it this way:
someString = someString.replacingOccurrences(of: "[abc]", with: "", options: [.regularExpression, .caseInsensitive])
As Martin R says, your string "Optional("5")" looks like you did something wrong.
dasblinkenlight answers you so it is fine, but for future readers, I will try to add alternative code as:
if let realString = yourOriginalString {
text2 = realString
} else {
text2 = ""
}
text2 in your example looks like String and it is maybe already set to "" but it looks like you have an yourOriginalString of type Optional(String) somewhere that it wasn't cast or use correctly.
I hope this can help some reader.
Swift 5 (working). Only 1 line code.
For removing single / multiple characters.
trimmingCharacters(in: CharacterSet)
In action:
var yourString:String = "(\"This Is: Your String\")"
yourString = yourString.trimmingCharacters(in: ["("," ",":","\"",")"])
print(yourString)
Output:
ThisIsYourString
You are entering a Set that contains characters you're required to trim.
Let's say you have a string:
var string = "potatoes + carrots"
And you want to replace the word "potatoes" in that string with "tomatoes"
string = string.replacingOccurrences(of: "potatoes", with: "tomatoes", options: NSString.CompareOptions.literal, range: nil)
If you print your string, it will now be: "tomatoes + carrots"
If you want to remove the word potatoes from the sting altogether, you can use:
string = string.replacingOccurrences(of: "potatoes", with: "", options: NSString.CompareOptions.literal, range: nil)
If you want to use some other characters in your sting, use:
Null Character (\0)
Backslash (\)
Horizontal Tab (\t)
Line Feed (\n)
Carriage Return (\r)
Double Quote (\")
Single Quote (\')
Example:
string = string.replacingOccurrences(of: "potatoes", with: "dog\'s toys", options: NSString.CompareOptions.literal, range: nil)
Output: "dog's toys + carrots"
If you are getting the output Optional(5) when trying to print the value of 5 in an optional Int or String, you should unwrap the value first:
if value != nil
{ print(value)
}
or you can use this:
if let value = text {
print(value)
}
or in simple just 1 line answer:
print(value ?? "")
The last line will check if variable 'value' has any value assigned to it, if not it will print empty string
You've instantiated text2 as an Optional (e.g. var text2: String?).
This is why you receive Optional("5") in your string.
take away the ? and replace with:
var text2: String = ""
If you are getting the output Optional(5) when trying to print the value of 5 in an optional Int or String, you should unwrap the value first:
if let value = text {
print(value)
}
Now you've got the value without the "Optional" string that Swift adds when the value is not unwrapped before.

Resources