String Methods in Swift

Level 2116 keystrokes

Removes the spaces at both ends with trimmingCharacters(in:) and checks for a substring with contains. trimmingCharacters(in:) comes from Foundation.

Swift code

// Foundation provides trimmingCharacters(in:)
1import Foundation
// A string with two spaces at each end
2let s = " Hello Swift "
// Foundation method: removes leading/trailing spaces and tabs, not inner ones: Hello Swift
3print(s.trimmingCharacters(in: .whitespaces))
// contains checks for the substring (case-sensitive): true
4print(s.contains("Swift"))

Output

Hello Swift
true
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Swift code and how often they appear.

  • "× 4
  • (× 4
  • )× 4
  • .× 3
  • :× 1
  • =× 1

More Swift examples at level 2

← Swift typing practice