How to calculate string length without spaces in Swift

1 Answer

0 votes
import Foundation

let str = "Swift Programming Language"

let stringWithoutSpaces = str.filter { !$0.isWhitespace }

let lengthWithoutSpaces = stringWithoutSpaces.count

print("Length without spaces: \(lengthWithoutSpaces)")



/*
run:

Length without spaces: 24

*/

 



answered Jan 11, 2025 by avibootz
...