2015-09-05 16 views

risposta

7

due soluzioni colata a NSString

let string = "Hello" 
let firstChar1 = string.substringToIndex(string.startIndex.successor()) 

let firstChar2 = string.characters.first 

Aggiornamento per Swift 2:

Poiché Swift 2 restituisce Character anziché String, è necessario creare un nuovo String.

let firstChar2 = String(string.characters.first!) 

Aggiornamento per Swift 3:

successor() è stato sostituito con index(after:..)

let firstChar1 = string.substring(to:string.index(after: string.startIndex)) 
+0

sì, casting non è necessario, perché è necessario Fondazione importazione, bridging così libero fa il lavoro – user3441734

4

Prova questo,

let str = "hogehoge" 
let text = (str as NSString).substringFromIndex(1) // "ogehoge" 
2

Prova questo

let myString = "My String" as NSString 
myString.substringWithRange(NSRange(location: 0, length: 3)) 
0
var mySuperCoolString = "Hello, World!!!!!!!!1!!";  
println(mySuperCoolString.substringWithRange(Range<String.Index>(start: advance(mySuperCoolString.startIndex, 0), end: advance(mySuperCoolString.startIndex, 1)))); 

Questo dovrebbe stampare H

2

In Swift 2 a String non è una raccolta di nulla. Secondo la documentazione:

/// `String` is not itself a collection of anything. Instead, it has 
/// properties that present the string's contents as meaningful 
/// collections: 
/// 
/// - `characters`: a collection of `Character` ([extended grapheme 
///  cluster](http://www.unicode.org/glossary/#extended_grapheme_cluster)) 
///  elements, a unit of text that is meaningful to most humans. 
/// 
/// - `unicodeScalars`: a collection of `UnicodeScalar` ([Unicode 
///  scalar 
///  values](http://www.unicode.org/glossary/#unicode_scalar_value)) 
///  the 21-bit codes that are the basic unit of Unicode. These 
///  values are equivalent to UTF-32 code units. 
/// 
/// - `utf16`: a collection of `UTF16.CodeUnit`, the 16-bit 
///  elements of the string's UTF-16 encoding. 
/// 
/// - `utf8`: a collection of `UTF8.CodeUnit`, the 8-bit 
///  elements of the string's UTF-8 encoding. 

Supponendo che si desidera trovare il secondo carattere,

var str = "Hello, playground" 
let chars = str.characters 
let n = 2 
let c = str.characters[str.characters.startIndex.advancedBy(n)] 
+0

Esattamente quello che stavo cercando, grazie! – mservidio

+0

@mservidio usa alternativamente chars = str.characters.map {$ 0}; sia c = chars [2]; La sottostringa – user3441734

4

Per quel che vale (e per coloro che cercano e trovare questo argomento), senza proiettare la stringa da NSString , è necessario effettuare le seguenti operazioni con Swift 2.1:

let myString = "Example String" 
let mySubString = myString.substringWithRange(Range<String.Index>(start: myString.startIndex.advanceBy(0), end: myString.startIndex.advanceBy(4))) 

print(mySubString) //'Exam' 

Questo stampa "Esame". Devo dire che è molto più prolisso che in Obj-C. E questo sta dicendo qualcosa ... ;-) Ma ha fatto il lavoro e senza lanciare su NSString.

+0

WithRange richiede l'importazione di base. Non è necessario eseguire il cast perché il bridging libero fa effettivamente il lavoro – user3441734

-1

Swift 2.2 e Swift 3,0

let string = "12134" 
string.substringWithRange(string.startIndex..<string.startIndex.advancedBy(2)) 
+0

per Swift 3: String.substring (con: Intervallo ) – Sasho

Problemi correlati