I have a string line from EditText, all words separated from each other by comma just like this:
"Some, string, words, bla-bla"
And I need to create array from this string, each element would be separate:
"Some", "string", "words", "bla-bla"
So, I saw how it was done on Java here but idk how convert it to kotlin, as I know Kotlin doesn't have split operator
This array I will save in preferences by putStringSet and use in another activity
Kotlin has a split function too. It will return a List<String>. If you you need an array in the end, call toTypedArray():
val strArray: Array<String> = "Some, string, words, bla-bla".split(", ").toTypedArray()
Related
I have a small problem. I created a large array.
It looks like this:
var Array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
If we write this way: Array[0] that shows all the elements.
If we write this way: Array[0][0] that shows "text1".
If we write this way: Array[0][2] that shows
-2 elements
-- 0: "text01"
-- 1: "text02"
.
If we write this way: Array[0][2].count or Array[0][2][0] it will not work
How do I choose each item, I need these elements for the tableView
The problem basically is that your inner array is illegal. Swift arrays must consist of elements of a single type. You have two types of element, String and Array Of String. Swift tries to compensate but the result is that double indexing can’t work, not least because there is no way to know whether a particular element will have a String or an Array in it.
The solution is to rearchitect completely. If your array entries all consist of the same pattern String plus String plus Array of String, then the pattern tells you what to do; that should be a custom struct, not an array at all.
as #matt already answered but I want to add this thing
Why Array[0][2].count or Array[0][2][0] not work
If you Define array
var array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
And when you type array you can see it's type is [[Any]] However it contains String as well as Array
So When you try to get Array[0][2] Swift does not know that your array at position 2 has another array which can have count
EDIT
What you are asking now is Array of dictionary I suggest you to go with model i.e create struct or class and use it instead of dictionary
Now If you want to create dictionary then
var arrOfDict = ["text10" : ["text01", "text02"] , "text11" : ["text11", "text12"]]
And you can access with key name let arrayatZero = arrOfDict["text10"] as? [String]
How to slice certain character for the selected object NOT string from array. Example,
date=[2017121918,2017122006,2017122012]
print date[1] : answer is 2017122006
My intention is to have only 122006.
If you can you convert it to a string first, you can do this:
str(date[1])[4:]
I'm just getting started in ruby and I have some trouble understanding the scan method:
my_string = "0000000001000100"
string_group = my_string.scan('/...../')
puts string_group
puts string_group.class
It displays that I've got an array but the array is empty. It can't come from my regex because I tested it and tried with another one:
'/[01]{5}/'
Why do I get an empty array?
Because regexes in Ruby are literal, not strings -- you have single quotes around your regex, so scan is searching for the literal string /...../ rather than matches to the regex /...../. Try this instead:
my_string = "0000000001000100"
string_group = my_string.scan(/...../)
Which gives this:
["00000", "00001", "00010"]
This question already has answers here:
How does String.Index work in Swift
(4 answers)
Closed 6 years ago.
I just started with Swift a few days ago and I am struggling with all the different Datatypes...
Lets say we do this:
var myarray = ["justin", "steve", "peter"]
var newString = myarray[2]
so why I cant now print just the "p" from "peter"?
print(newString[0])
---> gives me an error:
"'subscript' is unavailable: cannot subscript String with an Int"
in this topic:
[Get nth character of a string in Swift programming language
it says:
"Note that you can't ever use an index (or range) created from one string to another string"
But I cant imagine, that there isn't way to handle it...
Because When I do this:
var myarray = ["justin", "steve", "p.e.t.e.r"]
var newString = myarray[2]
let a : [String] = newString.componentsSeparatedByString(".")
print(a[2])
then it works. It prints (in this case) "t".
So how I can split it "SeparatedByString" 'Nothing'?
Im sure to solve this will help me also at many other problems.
I hope I postet the question in the way it should be done.
Thank you for any solution or tips :)
The instance method componentsSeparatedByString applied to a String instance yields an array of String instances, namely [String]. The elements of an array can be indexed in the classic Java/C++ fashion (let thirdElement = array[2]).
If you'd like to split your String instance ("peter") into an array of single character String instances, you can make use of the CharacterView property of a String instance, and thereafter map each single Character in the CharacterView back to a single-character String instance
let str = "peter"
let strArray = str.characters.map(String.init(_:)) // ["p", "e", "t", "e", "r"]
let thirdElement = strArray[2] // t
This is quite roundabout way though (String -> CharacterView -> [String] -> String), and you're most likely better off simply looking at the excellent answer (covering direct String indexing) in the dupe thread dug up by #Hamish:
How does String.Index work in Swift 3
The possible corner case where the above could be applicable, as pointed out by Hamish below, is if you very frequently need access to characters in the String at specific indices, and like to make use of the O(1) random access available to arrays (more so applicable if you are working with immutable String:s, such that the corresponding String array only needs to be generated once for each immutable String instance).
I have a program in Ada95, in which I have to create an array of strings. This array can contain strings of variable length.
Example:
I have declared the array in which all the indexes can store strings of size 50. When I assign a smaller string to the above array, I get "Constraint Error".
Code:
procedure anyname is
input_array : array(1..5) of String(1..50);
begin
input_array(1):="12345";
end anyname;
I have tried to create the array of Unbounded_Strings. But that doesn't work either. Can anyone tell me how to store this "12345" in the above string array?
If you use Unbounded_String, you cannot assign a string literal to it directly. String literals can have type String, Wide_String, or Wide_Wide_String, but nothing else; and assignment in Ada usually requires that the destination and source be the same type. To convert a String to an Unbounded_String, you need to call the To_Unbounded_String function:
procedure anyname is
input_array : array(1..5) of Ada.Strings.Unbounded.Unbounded_String;
begin
input_array(1) := Ada.Strings.Unbounded.To_Unbounded_String ("12345");
end anyname;
You can shorten the name by using a use clause; some other programmers might define their own renaming function, possibly even using the unary "+" operator:
function "+" (Source : String) return Ada.Strings.Unbounded.Unbounded_String
renames Ada.Strings.Unbounded.To_Unbounded_String;
procedure anyname is
input_array : array(1..5) of Ada.Strings.Unbounded.Unbounded_String;
begin
input_array(1) := +"12345"; -- uses renaming "+" operator
end anyname;
Not everyone likes this style.
You can use Ada.Strings.Unbounded, illustrated here, or you can use a static ragged array, illustrated here. The latter approach uses an array of aliased components, each of which may have a different length.
type String_Access is access constant String;
String_5: aliased constant String := "12345";
String_6: aliased constant String := "123456";
String_7: aliased constant String := "1234567";
...
Input_Array: array (1..N) of
String_Access :=
(1 => String_5'Access,
2 => String_6'Access,
3 => String_7'Access,
-- etc. up to N
);
Strings in Ada are arrays of characters of fixed length. In order to use strings of variable length (which may often be the case when arrays of strings are needed, e.g. arrays of names, each name being of variable length), each individual string may be declared as an Unbounded_String. The only caveat is that this allocates from the heap memory. Below is a complete example of an array of strings in Ada.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
procedure arrayAda is
type DaysArray is array(1..7) of Unbounded_String;
days: DaysArray;
begin
days(1):=To_Unbounded_String("Sunday");
days(2):=To_Unbounded_String("Monday");
days(3):=To_Unbounded_String("Tuesday");
days(4):=To_Unbounded_String("Wednesday");
days(5):=To_Unbounded_String("Thursday");
days(6):=To_Unbounded_String("Friday");
days(7):=To_Unbounded_String("Saturday");
for index in 1..7 loop
Put(days(index));
Put(" ");
end loop;
end arrayAda;
This produces the following output:
$ ./arrayAda
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
I've had a lot of joy from instantiating a container package, e.g.:
package String_Vectors is
new Ada.Containers.Indefinite_Vectors (Positive, String);
It's still a bit fiddly, compared to how easy it is to mess about with strings in a lot of other programming languages, but it's okay.
Fundamentally, Ada is a language designed to be usable without using the heap (at all :-) Most other languages would fall down in a, well, a heap, without the heap.