Definition of a string
var string = "Hello,swift";
Let string1 = "hello,swift";
Let string3 = "";
Let String4 = string;
print (string);
Print (string.uppercased ());//Uppercase
Print (string.lowercased ());//lowercase
String byte
Let Leng = String.lengthofbytes (Using:String.Encoding.utf8);
Print (leng);
Get string length
Let leng2 = String1.characters.count;
Print (Leng2,string3.characters.count,string4.characters.count);
String traversal
For Char in string1.characters{
print (char);
}
string concatenation
string + = "Liping";
print (string);
Let STR5 = String3 + string;
Print ("str5=", STR5);
Let STR6 = [NSString. Localizedstringwithformat ("%@,%@,%@", "Hello", "development", "Please Treat")];
Print (STR6)
Append character
String.append ("!")
print (string);
String interception
Let SubStr = (string as NSString). SUBSTRING (With:nsmakerange (1, 6));
Let SUBSTR2 = (string as NSString). substring (from:5);
Let SUBSTR3 = (string as NSString). substring (to:5);
Print ("substr=", SubStr, "substr2=", SubStr2, "substr3=", SUBSTR3)
string comparison
Let s1:nsstring = "Hello, Swift"
Let s2:nsstring = "Hello, li Ping";
If S1.isequal (to:s2 as String) {
Print ("S1=s2");
}else{
Print ("S1!=s2");
}
To compare the prefix suffix of a string
Let array = ["Do.docx", "Good.docx", "Name.docx", "Data.json", "Good.json"];
For ARRSTR in array{
If Arrstr.hasprefix ("good") {
Print ("prefix good:\ (ARRSTR)");
}
If Arrstr.hassuffix (". Json") {
Print ("suffix. Json:\ (arrstr)");
}
}
String Lookup
Let s3:nsstring = "Swift";
if (S1 as NSString). Contains (S3 as String) {
Print ("S1 exists inside S3");
}else{
Print ("does not exist");
}
Digitizing strings
Let S4 = "789";
Print ("Digitizing: \ ((S4 as NSString). Intvalue)");
Swift String Common methods