How to remove the last character from a string in Swift

1 Answer

0 votes
import Foundation

func removeLastChar(_ s: String?) -> String? {
    guard let s = s, !s.isEmpty else {
        return nil
    }
    
    return String(s.dropLast())
}

var s: String? = "swift java c c++ python"
        
s = removeLastChar(s)
        
print(s ?? "nil")



 
/*
run:
 
swift java c c++ pytho
 
*/

 



answered Dec 5, 2024 by avibootz
...