How to add a digit to a string that is represented always using two characters? - swift

I want to align some digits and numbers (which will be Strings) one below of another:
1
2
3
4
5
6
7
8
9
10
I want every digit from 1-9 to have a space in front so then it will look like this - (10 should have the 0 digit below 9, and not 1 digit below 9):
1
2
3
4
5
6
7
8
9
10
In the second case it looks more like right aligned.
Is there a way in Swift to do this ?

Use String(format:) with a field width of 2:
for i in 1...10 {
print(String(format: "%2d", i))
}
You can learn more about the String Format Specifiers here.

1- give them all equal leading
2- make equal width constraint for all
3- right align labels from 1-9

Apples NumberFormatter is what you want to use here.
let numbers:[NSNumber] = [1,2,3,4,5,6,7,8,9,10,11,12,100,1000]
let formatter = NumberFormatter()
formatter.minimumIntegerDigits = 2
for n in numbers {
print(formatter.string(from: n))
}
// prints 01, 02, 03 etc.
edit:
Ah, I didn't read the question properly, you want a space instead of a 0. Nevermind this answer :)

Related

How is this MATLAB code (involving colon operator) resolved?

Recently, I wanted to calculate the next multiple of 5 of several values.
I was very confused by the output of this code, which should have done the trick:
7:11 - mod(7:11, 5) + 5
ans =
7 8 9 10 11 12 13 14
While the actual working solution was this:
(7:11) - mod(7:11, 5) + 5
ans =
10 10 10 15 15
So this seems to be related to operator precedence! But what exactly does the first command do, and why does it output a (1,8) vector?
Addendum: I have found that the first command can also be written as:
7:(11 - mod(7:11, 5) + 5)
Which already hints towards the explanation of the observed result, but I am still curious about the whole explanation.
Here's the list of MATLAB operator precedence
As you can see, parentheses, (), are solved first, meaning that mod(7:11,5) will be done first. Then point 6), the addition and subtraction are taken care of from left to right, i.e. 11-mod(7:11,5) and then 11-mod(7:11,5)+5. Then point 7), the colon, :, gets evaluated, thus 7:11-mod(7:11,5)+5.
As you noted correctly 7:11 - mod(7:11, 5) + 5 is the same as 7:(11 - mod(7:11, 5) + 5), as seen above using operator precedence.
Now to the second part: why do you obtain 8 values, rather than 5? The problem here is "making an array with an array". Basically:
1:3
ans =
1 2 3
1:(3:5)
ans =
1 2 3
This shows what's going on. If you initialise an array with the colon, but have the end point as an array, MATLAB uses only the first value. As odd as it may sound, it's documented behaviour.
mod(7:11,5) generates an array, [2 3 4 0 1]. This array is then subtracted from 11 and 5 is added [14 13 12 16 15]. Now, as we see in the documentation, only the first element is then considered. 7:[14 13 12 16 15] gets parsed as 7:14 and will result in 8 values, as you've shown.
Doing (7:11) - mod(7:11, 5) + 5 first creates two arrays: 7:11 and mod(7:11,5). It then subtracts the two arrays elementwise and adds 5 to each of the elements. Interesting to note here would be that 7:12 - mod(7:11, 5) + 5 would work, whereas (7:12) - mod(7:11, 5) + 5 would result in an error due to incompatible array sizes.

How to split a 6 digit number into one column with 4 digits and one column with 2 digits (for example: 201452 into 2014 and 52)

How to split a 6 digit number into one column with 4 digits and one column with 2 digits (for example: 201452 into 2014 and 52) in PySpark. It should systematically split the 6 digit numbers after the fourth digit.
I already tried the pyspark.split(...) method but there I am not able to split the 6 digit numbers based on the position. How can I specify this position?
split_col=py.sql.functions.split(lambda x: df_datetime["WEEKNR"], '4')
df_datetime=df_datetime.withColumn('Name 1', split_col.getItem(0)).show()
You are just looking for the function substring
from pyspark.sql import functions as F
df.withColumn(
"four_let",
F.substring(F.col("WEEKNR"), 1, 4)
)

Applescript: return specific index positions from a date string

I have already used the text delimiters and item numbers to extract a date from a file name, so I'm clear about how to use these. Unfortunately the date on these particular files are formatted as "yyyyMMdd" and I need to covert the date into format "yyyy-MM-dd". I have been trying to use the offset function to get particular index positions, and I have found several examples of how you would return the offset of particular digits in the string, example:
set theposition to offset of 10 in theString -- this works
(which could return 5 or 7) but I have not found examples of how to call the digits at a specific index:
set _day to offset 7 of file_date_raw -- error
"Finder got an error: Some parameter is missing for offset." number -1701
How would you do this, or is there a totally better way I'm unaware of?
To "call the digits at a specific index", you use:
text 1 thru 4 of myString
If you know that each string has 8 characters in the yyyymmdd format, then you don't need to use 'offset' or any parsing, just add in the -'s, using text x thru y to dissect the string.
set d to "20011018"
set newString to (text 1 thru 4 of d) & "-" & (text 5 thru 6 of d) & "-" & (text 7 thru 8 of d)

How do I read last 20 digits of string in swift

I have a swift program in whom I need to read the last 20 digits of a string.
Although I would prefer the last 20 digits the first 20 would also be fine if it makes it any easier.
And a way to read all Digits except for the last 20.
You can use suffix:
String(yourString.characters.suffix(20))
It's interesting because the place you'd expect to find the answer would be the string functions -- where is the Swift equivalent of Javascript's String.substr() for example.
What you want is
String str = ...
str.substringFromIndex(advance(str.startIndex, 20)) // first 20 chars
str.substringFromIndex(advance(str.endIndex, -20)) // last 20 chars
In any case, you'll need to check if the str has fewer than 20 characters and just return the string itself.
You can determine the string length by
count(str) (older Swift versions) or str.characters.count (Swift 1.2)

Matlab, how to convert a string of integers into a vector? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
convert string to number array in matlab
Is there a simple way in Matlab to convert a string like this
'123456789'
into a vector like this ?
[1 2 3 4 5 6 7 8 9]
If all you have is contiguous characters from 0 to 9:
v = double(s)-'0';
double(s) converts a string into an array where each element is the ASCII code of the corresponding character. To obtain the numberic values we subtract '0' (which is in fact 48 in ASCII) and since digits have a sequential representation in ASCII code ('1' = 49, '2' = 50, etc.) we end up with intended result.
one way would be using regexp for this. But of course it only works for single digit numbers.
>> str = '123456789';
>> num = regexp(str,'\d')
num =
1 2 3 4 5 6 7 8 9