TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift Padding String Example (toLength)

Add padding to a String with the padding method and the toLength, withPad, startingAt arguments.
Padding. Sometimes a String needs to be a certain number of characters long. It must fill an entire text field. With padding methods, we can add characters to a String.
In Foundation, we find a method called padding() with 3 arguments. We specify a string that is added to fill the desired length. This pads a string.
First example. To begin, we introduce a String that has 4 characters (cats). We then invoke the padding() method. We use 3 arguments.

ToLength: We pass 10 to expand the string to a width of 10 characters. The resulting string will have 10 chars in its padded form.

WithString: This string is used to create the padding. It is repeated to fill the desired length.

Swift program that uses padding on string import Foundation // An example string. let cats = "cats" // Pad to length 10. let padded = cats.padding(toLength: 10, withPad: " ", startingAt: 0) // Write result. print("Padded = |\(padded)|") // Print before and after lengths. print(cats.endIndex) print(padded.endIndex) Output Padded = |cats | Index(_base: Swift.String.UnicodeScalarView.Index(_position: 4), _countUTF16: 0) Index(_base: Swift.String.UnicodeScalarView.Index(_position: 10), _countUTF16: 0)
Different characters. With the padding() method, we can pad with different characters. A space is a common padding char, but here we use a hyphen to pad a string.
Swift program that pads with hyphen character import Foundation // A string. let id = "X1" // Pad with hyphens. let padded = id.padding(toLength: 4, withPad: "-", startingAt: 0) // Uses hyphen as padding character. print(padded) Output X1--
Some notes. In Swift, strings can be hard to manipulate. But we usually do best by choosing the most specific Foundation method possible.

And: We could apply padding by adding space chars, but this is more complex. A single Foundation call is clearer.

Swift 3 changes. In Swift 3 we use padding() instead of stringByPaddingToLength. The method call is simpler, and the arguments are labeled differently.
A review. Padding increases the length of a String to fill a field. This can be used to ensure a string meets a length requirement. It can be used sometimes for text justification.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf