Skip to main content
updated advance to advancedBy(n) for Swift 2
Source Link

You can use the substringWithRange method. It takes a start and end String.Index.

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground" 

To change the start and end index, use advanceadvancedBy(n).

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: advance(str.startIndex, .advancedBy(2), end: advance(str.endIndex, .advancedBy(-1))) //"llo, playgroun" 

You can also still use the NSString method with NSRange, but you have to make sure you are using an NSString like this:

let myNSString = str as NSString myNSString.substringWithRange(NSRange(location: 0, length: 3)) 

Note: as JanX2 mentioned, this second method is not safe with unicode strings.

You can use the substringWithRange method. It takes a start and end String.Index.

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground" 

To change the start and end index, use advance().

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: advance(str.startIndex, 2), end: advance(str.endIndex, -1))) //"llo, playgroun" 

You can also still use the NSString method with NSRange, but you have to make sure you are using an NSString like this:

let myNSString = str as NSString myNSString.substringWithRange(NSRange(location: 0, length: 3)) 

Note: as JanX2 mentioned, this second method is not safe with unicode strings.

You can use the substringWithRange method. It takes a start and end String.Index.

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground" 

To change the start and end index, use advancedBy(n).

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex.advancedBy(2), end: str.endIndex.advancedBy(-1))) //"llo, playgroun" 

You can also still use the NSString method with NSRange, but you have to make sure you are using an NSString like this:

let myNSString = str as NSString myNSString.substringWithRange(NSRange(location: 0, length: 3)) 

Note: as JanX2 mentioned, this second method is not safe with unicode strings.

added 7 characters in body
Source Link
Connor
  • 64.8k
  • 28
  • 148
  • 145

You can use the substringWithRange method. It takes a start and end String.Index.

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground" 

To change the start and end index, use advance().

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: advance(str.startIndex, 2), end: advance(str.endIndex, -1))) //"llo, playgroun" 

You can also still use the NSString method with NSRange, but you have to make sure you are using an NSString like this:

let myNSString = str as NSString myNSString.substringWithRange(NSRange(location: 0, length: 3)) 

Note: as JanX2 mentioned, this second method is not safe with unicode strings.

You can use the substringWithRange method. It takes a start and end String.Index.

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground" 

To change the start and end index, use advance().

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: advance(str.startIndex, 2), end: advance(str.endIndex, -1))) //"llo, playgroun" 

You can also still use the NSString method with NSRange, but you have to make sure you are using an NSString like this:

let myNSString = str as NSString myNSString.substringWithRange(NSRange(location: 0, length: 3)) 

Note: as JanX2 mentioned, this method is not safe with unicode strings.

You can use the substringWithRange method. It takes a start and end String.Index.

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground" 

To change the start and end index, use advance().

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: advance(str.startIndex, 2), end: advance(str.endIndex, -1))) //"llo, playgroun" 

You can also still use the NSString method with NSRange, but you have to make sure you are using an NSString like this:

let myNSString = str as NSString myNSString.substringWithRange(NSRange(location: 0, length: 3)) 

Note: as JanX2 mentioned, this second method is not safe with unicode strings.

deleted 553 characters in body
Source Link
Connor
  • 64.8k
  • 28
  • 148
  • 145

Well first of all, youYou can still use the NSStringsubstringWithRange method with NSRange, but you have to make sure you are using an NSString like this:

let myNSString = str as NSString myNSString.substringWithRange(NSRange(location: 0,length: 3)) 

I've had trouble with using Ranges on Swift strings as well. This is the best I've come up with:It takes a start and end String.Index.

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground" 

It runs fine, but I haven't figured out how use indices other thanTo change the start and end.

There is a succ() and pred() method on String.Index which returns the index before or after itself, but they don't take any arguments so it seems like there's no easy way to select different indicesuse advance().

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: advance(str.startIndex.succ().succ(, 2), end: advance(str.endIndex, -1))) // "llo, Playground"playgroun" 

Here'sYou can also still use the definition of String.IndexNSString method with NSRange, but you have to make sure you are using an NSString like this:

extension String : Collection { let structmyNSString Index= :str BidirectionalIndexas {NSString  func succ() -> StringmyNSString.Index func predsubstringWithRange() -> String.Index } var startIndex: String.Index { get } var endIndexNSRange(location: String.Index { get } subscript0, (ilength: String.Index3) -> Character { get } func generate() -> IndexingGenerator<String> } 

Note: as JanX2 mentioned, this method is not safe with unicode strings.

Well first of all, you can still use the NSString method with NSRange, but you have to make sure you are using an NSString like this:

let myNSString = str as NSString myNSString.substringWithRange(NSRange(location: 0,length: 3)) 

I've had trouble with using Ranges on Swift strings as well. This is the best I've come up with:

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) 

It runs fine, but I haven't figured out how use indices other than start and end.

There is a succ() and pred() method on String.Index which returns the index before or after itself, but they don't take any arguments so it seems like there's no easy way to select different indices.

str.substringWithRange(Range<String.Index>(start: str.startIndex.succ().succ(), end: str.endIndex)) // "llo, Playground" 

Here's the definition of String.Index:

extension String : Collection {  struct Index : BidirectionalIndex {  func succ() -> String.Index func pred() -> String.Index } var startIndex: String.Index { get } var endIndex: String.Index { get } subscript (i: String.Index) -> Character { get } func generate() -> IndexingGenerator<String> } 

You can use the substringWithRange method. It takes a start and end String.Index.

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground" 

To change the start and end index, use advance().

var str = "Hello, playground" str.substringWithRange(Range<String.Index>(start: advance(str.startIndex, 2), end: advance(str.endIndex, -1))) //"llo, playgroun" 

You can also still use the NSString method with NSRange, but you have to make sure you are using an NSString like this:

let myNSString = str as NSString myNSString.substringWithRange(NSRange(location: 0, length: 3)) 

Note: as JanX2 mentioned, this method is not safe with unicode strings.

added 522 characters in body
Source Link
Connor
  • 64.8k
  • 28
  • 148
  • 145
Loading
Source Link
Connor
  • 64.8k
  • 28
  • 148
  • 145
Loading