• Register
1 vote

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.

8 5 2
3,230 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"

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)
It will surely help you.
8 4
5,680 points

Related questions

0 votes
1 answer 20 views
Problem Hey, i just want to learn how to remove substring from a string object in C++
asked Dec 20, 2020 chris jordan 2.4k points
1 vote
1 answer 99 views
Problem: I wrote this code to find all occurrences of a substring in a string python: import re test_str = "all in all is not the all of all " test_sub = "all" print("The main string is : " + test_str) print("The substring to find : " + test_sub) print(" ... is not defined The first two ouput of this code is right but it throws error message after that. What should I do to run this code properly?
asked Apr 22, 2020 samhaz 5k points
0 votes
1 answer 48 views
Problem: I want to learn how can we get the specific numbers of characters from a string. For example get last 4 characters of a string php.
asked Dec 9, 2020 prog_learner 2.5k points
0 votes
1 answer 18 views
18 views asked Oct 2, 2020 SapperIt 1.2k points
0 votes
1 answer 13 views
0 votes
1 answer 19 views
Problem: Hi there! I just got an assignment in which I had to remove the first and last character from a string in Java. I did the task using substrings. I want to know what the simplest way is to do so? Also, mention other alternatives to using the substring method. Thanks!
asked Jan 8 Code Learner 5.9k points
1 vote
1 answer 16 views
Problem: Hello, good people of Kodlogs, I am a new student of Python programming language. The online course I am taking they are teaching me well. Every weekend they provide me with some problems to solve. This week, I have got the problem find all substrings of a string python. I don’t have any idea how to do this! I would appreciate your effort if you pull me up. Many thanks
asked Apr 5, 2020 Gavin 15.3k points
0 votes
1 answer 8 views
Problem: str1 is declared as “Apples are red”. Then str2 stores the substring of str1 that starts at position 11 and is of length 3. Also, str3 stores the substring of str1 that starts at position 0 and is of length 6. find solution of this statement.
asked Dec 15, 2020 Mashhoodch 9.1k points
0 votes
1 answer 26 views
Hi, I have been searching for how to remove a substring from a string in python.
asked Oct 22, 2020 psandprop 2.4k points