Swift String Common methods

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.