Swift Program to Remove Last Character from a String

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to remove last 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 last character from the string Swift provide various methods like removeLast(), dropLast(), etc. These methods remove the last 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 −

soda is cold

Method 1- Using removeLast() Function

Swift provide a in-built function named removeLast() to remove the last 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.removeLast()

Algorithm

Following is the algorithm −

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

  • Step 2 − Removing last character from the string using removeLast() function.

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

Example

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

import Foundation import Glibc var myString = "Sunil like momos" print("Original String:", myString) // Removing last character from the string myString.removeLast() print("New String:", myString)

Output

Original String: Sunil like momos
New String: Sunil like momo

Here, in the above code, we have a string "Sunil like momos". Now we remove the last character from the string that is s using removeLast() function. Hence the output is "Sunil like momo".

Method 2 - Using dropLast() Function

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

Syntax

Following is the syntax −

Str.dropLast(x: Int)

Here x represents the number of characters to be removed from the ending 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 last character from the string using dropLast() function and store the result into res variable.

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

Example 1

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

import Foundation import Glibc var myString = "clouds are white " print("Original String:", myString) // Removing last character from the string var res = myString.dropLast() print("New String:", res)

Output

Original String: clouds are white
New String: clouds are white

Here, in the above code, we have a string "clouds are white". Now we remove the last character from the string that is e using dropLast() function. Hence the output is "clouds are white".

Example 2

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

import Foundation import Glibc var myString = "clouds are white " print("Original String:", myString) // Removing last character from the string var res = myString.dropLast(4) print("New String:", res)

Output

Original String: clouds are white
New String: clouds are wh

Here, in the above code, we have a string "clouds are white". Now we remove the last 4 characters from the string using dropLast(4) function. Hence the output is "clouds are wh".

raja
Updated on 11-Oct-2022 11:59:12

Advertisements