Swift Program to trim a string from the left side


In Swift, we can adjust the size of the string by trimming some specified number of characters from the left side of the string. Or we can also trim the extra whitespaces which we do not want on the left side of the original string using the following methods.

Method 1: Trim a string from the left side

To trim a specified number of characters or a substring from the left side of the string we create a user-defined function which takes an input string and the length of the characters which we wanted to remove from the left side of the original string as arguments and return a resultant new string.

Example

Input: String = "Ram got first place" trimLength = 5

Output: "ot first place"

Here, in the resultant string, the first 5 characters are removed from the end of the string because the size of trimLength is 5.

Algorithm

  • Step 1 − Create a function that takes an input string and trimLength as arguments.

  • Step 2 − Now this function first checks if the trimLength is greater than or equal to the size of the input string. If yes, then return the original string.

  • Step 3 − If no, then it will use the index() function to find the new index which is “trimeLength” characters to the right of the string’s startIndex.

  • Step 4 − Then it creates a new string including all the characters starting from the new index till the end using the range operator.

  • Step 5 − Return the new resultant string.

  • Step 6 − Create a string.

  • Step 7 − Create a variable and store the length which we want to remove from the left side of the original string.

  • Step 8 − Call the function and pass string and trimLength as arguments in it.

  • Step 9 − Display output.

Example

In the following Swift program, we will trim the string from the left side. So we create a function which takes a string and the length which we want to trim from the left side as arguments. Then it will check if the trimlength is greater than or equal to the length of the string. If yes, then we do not need to trim the string, so it will return the original string. If not, then the function calculates the index from which we want to trim using the index() function. Then use String() initializer along with range parameter([trimIndex…]) to create a substring from the original string from the calculated trimIndex. Finally, display the updated string.

import Foundation
import Glibc

func trimLeftString(str: String, trimlength: Int)-> String{
   if trimlength >= str.count
   {
      return str
   }
   
   let trimIndex = str.index(str.startIndex,
   offsetBy: trimlength)

   let newStr = String(str[trimIndex...])
    
   return newStr
}

let s = "Ram likes Swift"
let result = trimLeftString(str: s, trimlength: 8)
print("Original String:", s)
print("String after trimming(left-hand side):", result)

Output

Original String: Ram likes Swift
String after trimming(left-hand side): s Swift

Method 2: Trim WhiteSpaces from the left side

In Swift, we can also remove whitespaces from the left side of the input string. So for that, we create a user-defined function which will take the input string as an argument and return a new string which does not contain whitespaces on the left side.

Example

Input: String = "  Ram got first place"
           
Output: "Ram got first place"

Here, the input string contains two white spaces on the left so using the function we create a new string which doesn't have white spaces on the right side.

Algorithm

  • Step 1 − Create a function that takes an input string as an argument.

  • Step 2 − Inside the function first create a copy of the original string.

  • Step 3 − Then run a while loop to remove the leading whitespace characters from the given string using the removeFirst() function.

  • Step 4 − Return the new resultant string.

  • Step 5 − Create a string.

  • Step 6 − Call the function and pass the input string as arguments in it.

  • Step 7 − Display output.

Example

In the following Swift program, we will trim white spaces from the left side. So we create a function which takes an input string as an argument. This function first creates a copy of the original string. Then it will run a while loop which repeatedly calls the removeFirst() function until the input string does not have any leading whitespace character. And display the resultant string.

import Foundation
import Glibc

func trimString(istr: String) -> String {
   var nStr = istr
    
   while nStr.hasPrefix(" ") {
      nStr.removeFirst()
   }
   return nStr
}

let oString = "       Sky Pink"
let resString = trimString(istr:oString)

print("Original string: \"\(oString)\"")
print("Trimmed string: \"\(resString)\"")

Output

Original string: "       Sky Pink"
Trimmed string: "Sky Pink"

Conclusion

So this is how we can trim a string from the left side. If you want to remove a substring or a set of characters from the left side of the original string then you can use method 1. Or if you only want to remove white spaces from the right side of the original string, then you can use method 2. Both methods work effectively and trim some characters from the left of the input string either whitespace characters or non-whitespace characters.

Updated on: 16-Jun-2023

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements