Swift Program to Remove First Character from a String

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to remove first character from a string.

A string is an ordered set of characters, for example, "MyTutorialsPoint". It is used to represent textual data. Swift support a String data type which is used to create a string type variable, or we can say to represent strings.

To remove first character from the string Swift provide various methods like removeFirst(), dropFirst(), etc. These methods remove the initial or index 0 character from the given string .

Below is a demonstration of the same −

Suppose our given input is −

MyString = soda is cold

The desired output would be −

oda is cold

Method 1- Using removeFirst() Function

Swift provide a in-built function named removeFirst() to remove the initial character from the given string. This method does not create a new string, it simply modify the original string.

Syntax

Following is the syntax −

Str.removeFirst()

Algorithm

Following is the algorithm −

  • Step 1 − Declare a variable of String type with value.

  • Step 2 − Removing first character from the string using removeFirst() function.

myString.removeFirst()
  • Step 3 − Print the output

Example

The following program shows how to remove the first character from the string.

import Foundation import Glibc var myString = "Always stay blessed my friend" print("Original String:", myString) // Removing first character from the string myString.removeFirst() print("New String:", myString)

Output

Original String: Always stay blessed my friend
New String: lways stay blessed my friend

Here, in the above code, we have a string "Always stay blessed my friend". Now we remove the first character from the string that is A using removeFirst() function. Hence the output is "lways stay blessed my friend".

Method 2 - Using dropFirst() Function

To remove the first character form the given string Swift support an in-built function named as dropFirst() function. This function removes the first character from the string and return a resultant sub string. This function also removes the specified number of characters from the start of the given string.

Syntax

Following is the syntax −

Str.dropFirst(x: Int)

Here x represents the number of characters to be removed from the starting of the given string. It is an optional parameter.

Algorithm

Following is the algorithm −

  • Step 1 − Declare a variable of String type with value.

  • Step 2 − Removing first character from the string using dropFirst() function and store the result into res variable.

var res = myString.dropFirst()
  • Step 3 − Print the output

Example 1

The following program shows how to remove the first character from the string.

import Foundation import Glibc var myString = "Sweta like coding " print("Original String:", myString) // Removing first character from the string var res = myString.dropFirst() print("New String:", res)

Output

Original String: Sweta like coding
New String: weta like coding

Here, in the above code, we have a string "Sweta like coding". Now we remove the first character from the string that is S using dropFirst() function. Hence the output is "weta like coding".

Example 2

The following program shows how to remove multiple characters from the beginning of the string.

import Foundation import Glibc var myString = "Sweta like coding " print("Original String:", myString) // Removing first character from the string var res = myString.dropFirst(3) print("New String:", res)

Output

Original String: Sweta like coding
New String: ta like coding

Here, in the above code, we have a string "Sweta like coding". Now we remove the starting 3 characters(or from index 0 to 2) from the string using dropFirst(3) function. Hence the output is "ta like coding".

raja
Updated on 11-Oct-2022 11:56:18

Advertisements