split a string to an array (each character) - Swift [duplicate] - arrays

This question already has answers here:
Convert Swift string to array
(14 answers)
Closed 6 years ago.
I've got a little problem. I want to split a String in an array of characters, where I can work on each character.
For example:
"Hello"
arrayofstring[0] ->H
arrayofstring[1] ->e
arrayofstring[2] ->l
arrayofstring[3] ->l
arrayofstring[4] ->o
I am thankful for every help.

Unless I'm missing something, it's easier than either of the other answers:
let arrayofstring = Array("Hello".characters)

Do something like this:
let string : String = "Hello"
let arrayofstring = string.characters.map { (Character) -> Character in
return Character
}
println(arrayofstring)

Related

How can I get a string from a 2d character array in C? [duplicate]

This question already has answers here:
What's the rationale for null terminated strings?
(20 answers)
Closed 3 years ago.
The 2d array:
char c[4][3]={{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'}};
As I wanted to get 'def' after running the program, I tried this code:
printf("%s\n",c[1]);
However, the result was 'defghijkl\262'. What's wrong with my code?
You can print def in two ways:
char c[4][4]={{'a','b','c','\0'},{'d','e','f','\0'},{'g','h','i','\0'},{'j','k','l','\0'}};
printf("%s\n",c[1]);
So, basically printf needs null termination to know where to stop printing
or you can print using a loop without null termination like:
char c[4][3]={{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'}};
for(int i = 0; i < 3; i++)
{
printf("%c", c[1][i]);
}

detect string at the end of pattern [duplicate]

This question already has answers here:
regex pattern to match the end of a string
(5 answers)
Closed 3 years ago.
I want to detect a string at the end of a pattern using C regex lib.
I'm looking for a string like ".mystring" with the litteral dot.
I expect the following to work :
char * pattern = "[[:alnum:]!--_]+\.(mystring)";
But it doesn't. What am I doing wrong here ?
You need to use $ end of string anchor
char * pattern = "[[:alnum:]!--_]+\.(mystring)$";
Also no need to of capture group here
char * pattern = "[[:alnum:]!--_]+\.mystring$";

Swift string to array of single character strings [duplicate]

This question already has answers here:
Convert Swift string to array
(14 answers)
Closed 4 years ago.
How can I convert a string into an array of single strings? So String -> [String]
If I do Array(myString) it results in an array of characters. I tried flatmap (or compactMap in the latest Xcode beta):
stringArray = Array(myString).flatMap { $0 as? String }
Then I get a warning: "Cast from 'Character' to unrelated type 'String' always fails" and `print(stringArray) results in [].
Is this possible?
Edit: this was already answered in a non-accepted answer in this question: Convert Swift string to array
You can do it like as follows... Initialize a new string inside the map.
let test = "thisIsATest"
let array = test.map( { String($0) })

How to convert a swift string to an array? [duplicate]

This question already has answers here:
Swift 3.0 iterate over String.Index range
(10 answers)
Convert Swift string to array
(14 answers)
Closed 5 years ago.
Would it be possible to loop through each character in the string, and then place each character into an array?
I'm new to swift, and I'm trying to figure this out. Could someone write a code for this?
It's really simple:
let str = "My String"
let letters = str.characters.map({String($0)})
print(letters)

Convert an array of Ints to a comma separated string [duplicate]

This question already has answers here:
How do I convert a Swift Array to a String?
(25 answers)
Closed 5 years ago.
I know that if I want to convert an array of Ints to a String, I do this:
[0,1,1,0].map{"\($0)"}.reduce(""){$0+$1}
but I cannot figure out how would I convert an array of Ints to a comma separated String
Well you can do it like :
let formattedArray = ([0,1,1,0].map{String($0)}.joined(separator: ",")

Resources