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

Related questions

1 answer 107 views
1 answer 111 views
1 answer 104 views
1 answer 86 views
1 answer 170 views
1 answer 88 views
...