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 119 views
1 answer 119 views
1 answer 116 views
1 answer 92 views
1 answer 186 views
1 answer 98 views
...