• Register
1 vote
240 views

Problem :

I am using Swift from some time. It was very useful for beginner like me to get a substring from String with the use of the characters property but now I came to know that it has been deprecated and now Xcode suggests to use the substring. I have already tried to do the research on it but couldn’t found any solution on it. Please find below the warning message for your reference:

'Characters' is deprecated: Please use String or Substring.

I am trying to find fix on above error from couple of days and in this process I have already wasted a lot of time. Now I need expert help to fix the issue.

3.2k points

Please log in or register to answer this question.

1 Answer

1 vote

Solution :

Now onwards you need to use this characters as the String stopped being used as a collection in Swift 2.0. But this is still a valid code in the Swift 4 and also now it is no longer necessary that the String is a Collection again.

Please find below example for your help the Swift 4 String now has the direct count property which will give you the character count as shown below:

// Swift 4
let spString = "Swift"
spString.count

Please find below the examples for String and SubString. Swift 4 String now will directly get the Element that gives you first character of String as shown below :

let spString = "Swift"
let firstElement = spString.first

Now using SubString you can get first character as shown below:

let spstring = "Swift"
let indexStartOfText = spstring.index(spstring.startIndex, offsetBy: 1)
let sub = spstring.substring(to: indexStartOfText)
print(sub)
It will surely help you.
5.7k points

Related questions

0 votes
1 answer 3 views
3 views
Problem: Check if string is substring of another python.
asked Jul 5 Rada Gracelynn 100k points
0 votes
1 answer 19 views
19 views
Problem: Please,I want an answer : Check if one string is substring of another
asked May 30 Sawyer Parviz 166k points
0 votes
1 answer 13 views
13 views
Problem: I can't seem to find a solution; could you please help me “Find if a string is a substring of another java”?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 3 views
3 views
Problem: Finding a substring in a string java?
asked 1 day ago Mezbah 125k points
0 votes
1 answer 4 views
4 views
Problem : C# check if string contains substring.
asked 4 days ago Isac Christiaan 102k points
0 votes
1 answer 8 views
8 views
Problem: Check if a string contains a substring?
asked Jul 24 Nahil 122k points
0 votes
1 answer 14 views
14 views
Problem: Bash check if string contains substring.
asked Jul 20 Florina Gulnar 103k points
0 votes
1 answer 13 views
13 views
Problem: Check if string contains substring java.
asked Jul 20 Florina Gulnar 103k points
0 votes
1 answer 12 views
12 views
Problem: Java check if string contains substring.
asked Jul 20 Florina Gulnar 103k points
0 votes
1 answer 2 views
2 views
Problem: See if a string contains a substring java
asked Jul 15 nila 117k points