- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift Program to Get a Character From the Given String
In Swift, we can easily get a character from the given string with the help of their respective index value. So to get the index of the specified character Swift provides an inbuilt function named index(). This function will return an index which is the specified distance from the specified index.
Example
Input: String = "Ram got first place"
index = 5 Output: "o"
Here, the given index value is 5 so the resultant character is “o”.
Syntax
func index(x:String.index, offsetBy: Int)
Here, x is the valid index of the sequence and offsetBy is the valid distance to offset x. And will return an index offset by distance from the index x.
Algorithm
Step 1 − Create a string.
Step 2 − Declare a variable to store the index of the requested character.
Step 3 − Now check if the given index is in the valid range or not.
Step 4 − If yes, then use the index() function to find the accurate index to access the character at the specified position and then use subscript notation to print the character.
Step 5 − If no, then display an error message.
Example 1
In the following Swift program, get a character from the given string. So for that, we first create a string and then specify the index of the character we want. Then we check if the given index is in the range or not. If yes, then we find the accurate index of the character using the index() function and display the resultant character using subscript notation. If not, then we will print an error message.
import Foundation import Glibc let inputString = "Birds fly in the morning" let charIndex = 4 if charIndex < inputString.count { let charOutput = inputString[inputString.index(inputString.startIndex, offsetBy: charIndex)] print("Character at index \(charIndex) is \(charOutput)") } else { print("Recheck your index it is out of range") }
Output
Character at index 4 is s
Example 2
In the following Swift program, get a character from the given string. So for that, we first create a string and then specify the index of the character we want. Then we calculate the accurate index of the character using the index() function. This function takes two arguments: the starting index(enteredString.StartIndex) and the offset by which to move the index. After that, we will use subscript notation to get the character from the specified index and display the output.
import Foundation import Glibc let enteredString = "This is Swift tutorial" let charIndex = 8 let resCharIndex = enteredString.index(enteredString.startIndex, offsetBy: charIndex) let myChar = enteredString[resCharIndex] print("Character at index \(charIndex) is \(myChar)")
Output
Character at index 8 is S
Example 3
In the following Swift program, get a character from the given string using prefix() and suffix() methods. So for that, we first create a string and then specify the index of the character we want. Then we will use prefix() method to extract substring from the beginning of the string upto the given index, then we use suffix() method to extract a substring containing of the last character. After that we will use first property to get the first character from the substring and display the output accordingly.
import Foundation import Glibc var StringVal = "Meeta is cooking Paratha" let charIndex = 4 if let char = StringVal.prefix(charIndex + 1).suffix(1).first { print("The character at \(charIndex)th Index is '\(char)'.") } else { print("Invalid character index. Try again") }
Output
The character at 4th Index is 'a'.
Conclusion
So this is how we can get a character from the given string. Both methods return accurate results if the given index is present in the valid range. If not then you will get an error. Without an index value, you will not get a character from the given string so always specify a valid index. Otherwise you will get error.