I using split method to split the String.
String date = "2020-10-07";
date.split("-");
print("split " + date[0]);
I expect will get 2020, but why it return 2 ?
The reason you are getting 2 is because it is the first position (0) of the string variable date.
When you split a string, it will return a list/array.
String date = "2020-10-07";
final dateList = date.split("-");
print("split " + dateList[0]);
//expected "split 2020"
It is 2 because there no change has been done to the variable date( the same without the split), but with split you can access the list like this below
String date = "2020-10-07";
var first = date.split("-").first;//returns the first element of the list
print("split " + first);
(Any String).split(Pattern) returns List<String>.
Since no operation was done to change the variable (it anyhow can't store List<String>).
You are left with:
Temporarily store the split as #Morgan Hunt and #Henok Suggested
print("split " + date.split("-").first); (I guess you are going to return the first split)
Also data[0] -> is an operation on a string hence you are getting 0th position -> 2
Related
I want start with empty list:
myList = []
and with some operation to create a list with 2 organs for 1 arguments, something like that:
myList = [['a',10],['b', 4],['g',142]]
I tried to do:
myList.append("[" + string + "," + str(number) + "]")
but when i do it, all of it saved as string and it save the value like this(with ""):
myList[0] = "['a',5]"
I like it to be:
myList[0] = ['a',5]
Try
myList.append(['a',5])
You're creating a string version of your input (+ on two strings just combines the strings into one string) but you just need to append the term you want, not create a string of it.
You could use
myList.append([string,number])
im having a hard time getting a function working. I need to search message.text for each "," found, for each "," found I need to get the number position of where the "," is located in the string. For example: 23232,111,02020332,12 it would return 6/10/19 where the "," are located (index of). My code finds the first index of the first , but then just repeats 6 6 6 6 over, any help would be appreciated thanks.
Heres my code:
For Each i As Char In message.Text
If message.Text.Contains(",") Then
Dim data As String = message.Text
Dim index As Integer = System.Text.RegularExpressions.Regex.Match(data, ",").Index
commas.AppendText(index & " ")
End If
Next
You can try it this way; instantiate a Regex object and increment each time the position from which you start the matching (this possibility is not available with the static method Match).
Dim reg As New System.Text.RegularExpressions.Regex(",")
Dim Index As Integer = reg.Match(data).Index
Do While Index > 0
commas.AppendText(index & " ")
Index = reg.Match(data, Index + 1).Index
Loop
p.s the returned indices are zero-based.
Just use the Regex.Matches method
Dim message As String = "23232,111,02020332,12"
Dim result As String = ""
For Each m As Match In Regex.Matches(message, ",")
result &= m.Index + 1 & " "
Next
I should also add that indexes are 0 based (which is why +1 is added to m.Index). If you later need these values to point to the position of a particular comma, you may be off by 1 and could potentially try to access an index larger than the actual string.
what I want to do is insert a desired character like a "space" into a desired string like "123456789" at a specific point. Example: insert a space at the position 5 in the string 123456789 = 1234 56789. Here is my code:
Dim str As String = sum2.Text '123456789
Dim insStr As String = " " 'space
Dim strRes As String = str.Insert(5, insStr) '5th position
the code looks fine and i dont get any errors when i use it or run it but it will not add the space at the 5th position so i need some help!
You must not that the startIndex in String.Insert(Integer, String) is zero based. That means if your intention is to insert a space in the 5th position, you will have to adjust that by -1:
Dim insertPosition = 5 ' Assuming this came from the user who says put it in position 5
Dim inStr = " "
Dim strRes = str.Insert(insertPosition - 1, inStr) ' assuming your str already had a value.
That will insert the space between 4 and 5 and will produce
1234 56789
I saw in one your comments that you might want to insert spaces in positions 1,5,7. In that case, you will have to do it in reverse, starting with the largest position, to the smallest. This is of cause assuming that you wanted
_1234_6_89
I have used underscores to represent spaces so that you can see it better.
Before working with that, ensure that your string has enough characters to be indexed by your index otherwise you'll get a ArgumentOutOfRangeException.
If I have a string, for example "Tiger," what could I write that would return T + i + g + e + r? It would be nice if I could put each letter inside of an array.
I need this because I'm writing a program that analyzes an inputted string and determines how many times repeated letters occur.
Try String.split() method with empty delimeter:
var str:String = "Tiger";
var letters:Array = str.split('');
//result-> ["T","i","g","e","r"]
I'm trying to merge cell-array of strings delimiting each by new line to one string in Matlab.
Following method merges the strings, but the final string contains \n instead of new lines:
function str = toString(self)
% some not important logic that creates cell array called strings
% ...
str = '';
for i = 1 : 9
str = strcat(str, strings(i), '\n');
end
end
It returns: ' 111\n 111\n 111\n333666444555\n333666444555\n333666444555\n 222\n 222\n 222\n'
When I add str = sprintf(str); before the end of the method, it returns Invalid format error. However when I write to Matlab command window sprintf(' 111\n 111\n 111\n333666444555\n333666444555\n333666444555\n 222\n 222\n 222\n'); it returns formatted string without any errors.
Anyone knows what could be a problem? Why it works in command window but doesn't in .m file?
sprintf will loop over the elements or your cell array:
sprintf('%s\n', strings{:})
The problem with your loop is '\n' is a 2 element char array, but what you want is sprintf('\n')