How to use Foundation.NSString class

Best Nimble code snippet using .Foundation.NSString

Run Nimble automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

string1.swift

Source: string1.swift Github

copy
1import Foundation
2
3//
4// 문자열 선언
5//
6
7var nsStr : NSString = "Hello, Foundation"
8// Swift의 String 타입의 문자열
9var swiftStr = "Hello, Swift"
10
11//
12// 문자열 변환
13//
14
15// NSString을 String으로 – as 필요
16var str1 : String = nsStr as String
17
18// Swift String에서 NSString으로 타입 변환
19var str2 : NSString = swiftStr as NSString
20
21
22//
23// 문자열 포맷
24//
25var formattedStr1 = NSString(format: "%d + %d = %d", 1, 2, (1+2))
26var formattedStr2 = NSString(format: "%@ %@", "Hello", "Foundation")
27
28//
29// 문자열 비교 : equality
30//
31
32
33let nsStr1 : NSString = "Hello Foundation"
34let nsStr2 : NSString = "Hello Foundation"
35
36let swiftStr2 = "Hello Foundation"
37
38print("NSString == ", nsStr1 == nsStr2)
39print("NSString === ", nsStr1 === nsStr2)
40print("NSString.isEqual(to:) : ", nsStr1.isEqual(to: swiftStr2))
41// Swift String에서 isEqual(to:) 사용 불가
42swiftStr2.isEqual(nsStr1)
43
44//
45// 문자열 비교 : compare
46//
47
48let numStr1 : NSString = "1"
49let numStr2 = "09"
50
51// Descending : “1” > “09”
52let ret1 = numStr1.compare(numStr2)
53print("1 compare 09 : isDescending", ret1 == ComparisonResult.orderedDescending)
54
55// Ascending : 1 < 09
56let ret2 = numStr1.compare(numStr2, options: .numeric)
57print("1 compare(numeric) 09 : isAscending", ret2 == ComparisonResult.orderedAscending)
58
59
60let ret3 = "HELLO".compare("hello", options: .caseInsensitive)
61
62print("HELLO compare(caseInsensitive) hello, isSame: ", ret3 == ComparisonResult.orderedSame)
63
64
65//
66// 문자열 길이
67//
68
69print("NSString.length : ", nsStr.length)
70print("Swift String.characters.count : ", swiftStr.characters.count)
71
Full Screen

TestNSString.swift

Source: TestNSString.swift Github

copy
1// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
2// Licensed under Apache License v2.0 with Runtime Library Exception
3//
4// See https://swift.org/LICENSE.txt for license information
5// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
6//
7//===----------------------------------------------------------------------===//
8//
9// RUN: %empty-directory(%t)
10//
11// RUN: %target-clang %S/Inputs/FoundationBridge/FoundationBridge.m -c -o %t/FoundationBridgeObjC.o -g
12// RUN: %target-build-swift %s -I %S/Inputs/FoundationBridge/ -Xlinker %t/FoundationBridgeObjC.o -sanitize=address -o %t/TestNSString
13// RUN: %target-codesign %t/TestNSString
14
15// RUN: %target-run %t/TestNSString > %t.txt
16// REQUIRES: executable_test
17// REQUIRES: asan_runtime
18// REQUIRES: objc_interop
19// REQUIRES: rdar55727144
20
21import Foundation
22import FoundationBridgeObjC
23
24#if FOUNDATION_XCTEST
25    import XCTest
26    class TestNSStringSuper : XCTestCase { }
27#else
28    import StdlibUnittest
29    class TestNSStringSuper { }
30#endif
31
32class TestNSString : TestNSStringSuper {
33    
34  func test_equalOverflow() {
35    let cyrillic = "чебурашка@ящик-с-апельсинами.рф"
36    let other = getNSStringEqualTestString()
37    print(NSStringBridgeTestEqual(cyrillic, other))
38  }
39  
40  func test_smallString_BOM() {
41    let bom = "\u{FEFF}" // U+FEFF (ZERO WIDTH NO-BREAK SPACE)
42//    expectEqual(1, NSString(string: bom).length)
43//    expectEqual(4, NSString(string: "\(bom)abc").length)
44//    expectEqual(5, NSString(string: "\(bom)\(bom)abc").length)
45//    expectEqual(4, NSString(string: "a\(bom)bc").length)
46//    expectEqual(13, NSString(string: "\(bom)234567890123").length)
47//    expectEqual(14, NSString(string: "\(bom)2345678901234").length)
48    
49    expectEqual(1, (bom as NSString).length)
50    expectEqual(4, ("\(bom)abc" as NSString).length)
51    expectEqual(5, ("\(bom)\(bom)abc" as NSString).length)
52    expectEqual(4, ("a\(bom)bc" as NSString).length)
53    expectEqual(13, ("\(bom)234567890123" as NSString).length)
54    expectEqual(14, ("\(bom)2345678901234" as NSString).length)
55    
56    let string = "\(bom)abc"
57    let middleIndex = string.index(string.startIndex, offsetBy: 2)
58    string.enumerateSubstrings(in: middleIndex..<string.endIndex, options: .byLines) { (_, _, _, _) in }  //shouldn't crash
59  }
60  
61  func test_unpairedSurrogates() {
62    let evil = getNSStringWithUnpairedSurrogate();
63    print("\(evil)")
64  }
65  
66}
67
68#if !FOUNDATION_XCTEST
69var NSStringTests = TestSuite("TestNSString")
70NSStringTests.test("test_equalOverflow") { TestNSString().test_equalOverflow() }
71NSStringTests.test("test_smallString_BOM") {
72  TestNSString().test_smallString_BOM()
73}
74NSStringTests.test("test_unpairedSurrogates") {
75  TestNSString().test_unpairedSurrogates()
76}
77runAllTests()
78#endif
79
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in Foundation.NSString

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)