Swift String removeFirst() Function



Swift provides a pre-defined structure named String. The String represents a collection of characters such as "HELLO". This structure provides various methods to modify the given string. The removeFirst() method is one of them. This method removes the characters from the beginning of the given string.

In Swift, we can write the removeFirst() method in two different ways-

  • removeFirst() method
  • removeFirst(_:) method

removeFirst() Method

The removeFirst() method is used to remove the first character of the given string. The string pass in this function must not be empty. If the input string is empty, then it will return an error.

Syntax

Following is the syntax of the removeFirst() function −

func removeFirst()

Parameters

This method does not take any parameters.

Return Value

The method returns the removed character.

Example 1

Swift program to remove the first character of the given string −

import Foundation

// Declaring a string
var str = "Lets learn Swift Programming"
print("Original String:", str)

// Removing the first character from the string
// Using removeFirst() method
str.removeFirst()

print("Modified String:", str);

Output

Original String: Lets learn Swift Programming
Modified String: ets learn Swift Programming

Example 2

Swift program to remove the first three characters from the string −

import Foundation
// Declaring a string
var str = "Welcome to my home Roma"
print("Original String:", str)

// Removing the first three characters from the string
// Using removeFirst() method
for _ in 0..<3{
   str.removeFirst()
}
print("Modified String:", str)

Output

Original String: Welcome to my home Roma
Modified String: come to my home Roma

removeFirst(_:) Method

The removeFirst(_:) method is used to remove the specified number of characters from the string. This method removes characters from the beginning of the given string and the passed string should not be empty, if the passed string is empty, then it will return an error.

Syntax

Following is the syntax of the removeFirst(_:) function −

func removeFirst(_x: Int)

Parameters

This method takes only one parameter that is, the total number of characters that we want to remove from the beginning of the string. The value of x must be greater than or equal to zero. And must not be greater than the size of the string.

Return Value

The method returns the removed characters.

Example 1

Swift program to remove the first character of the given string −

import Foundation
import Foundation

// Declaring a string
var str = "My car color is pink"
print("Original String:", str)

// Removing the first character from the string
// Using removeFirst(_:) method
str.removeFirst(1)

print("Modified String:", str)

Output

Original String: My car color is pink
Modified String: y car color is pink

Example 2

Swift program to remove the first 5 characters from the string −

import Foundation

// Declaring a string
var str = "TutorialsPoint is a great learning platform"
print("Original String:", str)

// Removing the first five characters from the string
// Using removeFirst(_:) method
str.removeFirst(5)

print("Modified String:", str)

Output

Original String: TutorialsPoint is a great learning platform
Modified String: ialsPoint is a great learning platform
swift_strings.htm
Advertisements