How to remove the last N characters from string in Swift

2 Answers

0 votes
var s = "swift java c++ c python"

let N = 4

let substring = s.dropLast(N)      

print(substring)
 
 
 
  
/*
run:

swift java c++ c py
  
*/

 



answered Nov 9, 2020 by avibootz
0 votes
var s = "swift java c++ c python"
 
let N = 4
 
s = String(s.dropLast(N))
 
print(s)
  
  
  
   
/*
run:
 
swift java c++ c py
   
*/

 



answered Nov 9, 2020 by avibootz
...