Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 682 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the length of a String

#1
How do you get the length of a `String`? For example, I have a variable defined like:

var test1: String = "Scott"

However, I can't seem to find a length method on the string.

Reply

#2
Here's something shorter, and more natural than using a global function:

aString.utf16count

I don't know if it's available in beta 1, though. But it's definitely there in beta 2.
Reply

#3
If you are just trying to see if a string is empty or not (checking for length of 0), Swift offers a simple boolean test method on `String`

myString.isEmpty

The other side of this coin was people asking in ObjectiveC how to ask if a string was empty where the answer was to check for a length of 0:

[To see links please register here]

Reply

#4
var str = "Hello, playground"
var newString = str as NSString

countElements(str)
This counts the characters in Regular Swift String

countElements((newString as String))
This counts the characters in a NSString
Reply

#5
Updated for Xcode 6 beta 4, change method utf16count --> utf16Count

var test1: String = "Scott"
var length = test1.utf16Count
Or

var test1: String = "Scott"
var length = test1.lengthOfBytesUsingEncoding(NSUTF16StringEncoding)



Reply

#6
If you are looking for a cleaner way to get length of a string checkout this library which has bunch of extensions to the Swift built in classes

[To see links please register here]


Using this library you can just do `"Some Str".length`
Reply

#7
As of Swift 1.2 `utf16Count` has been removed. You should now use the global `count()` function and pass the UTF16 view of the string. Example below...

let string = "Some string"
count(string.utf16)
Reply

#8
Best way to count String in Swift is this:

var str = "Hello World"
var length = count(str.utf16)
Reply

#9
String and NSString are toll free bridge so you can use all methods available to NSString with swift String

let x = "test" as NSString
let y : NSString = "string 2"
let lenx = x.count
let leny = y.count
Reply

#10
Using **Xcode 6.4**, **Swift 1.2** and **iOS 8.4**:

//: Playground - noun: a place where people can play

import UIKit

var str = " He\u{2606} "
count(str) // 7

let length = count(str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())) as Int // 3
println(length == 3) // true
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through