Get character from a string in Scala but keep the result as a string? - scala

Imagine that I wanted to take the characters from a string in Scala but have the toInt conversion to behave as it would on a string instead of as on a character.
To illustrate the following code behaves like so:
"0".toInt // results in 0
"000".charAt(0).toInt // results in 48
I'd like a version of the second line that would also result in 0. I have a solution like the following:
"000".charAt(0).toString.toInt // results in 0
But I wonder if there is a more direct or better way?

You can use asDigit:
val i: Int = "000".charAt(0).asDigit

You can do:
"000".substring(0, 1).toInt
But I'm not sure it's more "direct" than "000".charAt(0).toString.toInt

Related

How to convert a number into a string in Maple?

I want to have something like this code from Python
num=3
res=str(num)
but in Maple. I couldn't find any appropriate constructors for this. Are there any?
num:=3:
convert(num,string);
"3"
sprintf("%a",num);
"3"
The best way is to use convert as already exists in #acer's answer. Just to name one more possibility here is another way.
num := 3:
res := cat( "", num );
You will get "3" for res of type string. What cat here does is concatenating 3 to the empty string "", and when there exists at least one string in the arguments of cat, the output becomes a string. You can even have something like sqrt(2) instead of 3 in num, in that case res becomes this string; "2^(1/2)". But sometimes it may give you a non-string object, for example if the number in num is of the form RootOf. See the help page to read more.

How can I read characters of a String with String format in Swift?

I am trying use String(format:, ) for reading some characters from left or right, do we have something for this job?
for example reading 2 characters from left would be: "AB" like this: "%2L#"
my code:
let stringOfText = String(format: "%#", "ABCDEF")
String(format:) is usually to transform a different value type into a string.
Since you already have a string, you don't really need this method.
try:
https://developer.apple.com/documentation/swift/string/2894830-prefix

What is the fastest/easiest way to increase a number in a string variable in Powershell?

I have the following Powershell variable
$var = "AB-0045"
I would like to increase the number in the string to become "AB-0046".
I can do:
$newNumber = [int]$var.Substring($var.length -4,4) + 1
Which will give me the desired number 46, but then I have to append that 46 as a string to a new string "AB-00".
Is there a better way to do that?
Now that you have the integer, you'll have to convert back to string formatted in the way you'd like and concatenate.
I'd recommend adding to "AB-" rather than "AB-00" in case your number goes over 100.
To pad leading zeros, you can use the -f operator.
e.g. "{0:d4}" -f 45
You'll still need to get the integer first (45 in the example) from your original string.
I tested with regex class Replace() method and string class Split() method with string formatter. Split() seems faster provided your string is always in the same format. The Replace() method does not care what happens before the last 4 numbers:
# Replace Method
[regex]::Replace($var,'\d{4}$',{([int]$args[0].Value+1).ToString('0000')})
# Split method
$a,[int]$b = $var.split('-'); "{0}-{1:0000}" -f $a,++$b

How to represent large number without the E form in Scala

I deal with numbers of this form 1.446267186999E7 and i want to represent them without E.
For example 1.446267186999E7 i want it to be 14462671.86999 .
How do i convert it to this form without getting the :
error: integer number too large.
Thanks for the helpers.
Try this:
BigDecimal(1.446267186999E7).toString
The BigDecimal.toString method will give you the string representation of the number in decimal form.
That is just a formatting problem if you store it as a double.
import java.text.DecimalFormat
val d: Double = 1.446267186999E7
val decimalFormat: DecimalFormat = new DecimalFormat("0.#####")
println(decimalFormat.format(d))
should give you 14462671.86999
You probably after BigDecimal. In terms of string formatting, look at the .format method, or printf

How to strip everything except digits from a string in Scala (quick one liners)

This is driving me nuts... there must be a way to strip out all non-digit characters (or perform other simple filtering) in a String.
Example: I want to turn a phone number ("+72 (93) 2342-7772" or "+1 310-777-2341") into a simple numeric String (not an Int), such as "729323427772" or "13107772341".
I tried "[\\d]+".r.findAllIn(phoneNumber) which returns an Iteratee and then I would have to recombine them into a String somehow... seems horribly wasteful.
I also came up with: phoneNumber.filter("0123456789".contains(_)) but that becomes tedious for other situations. For instance, removing all punctuation... I'm really after something that works with a regular expression so it has wider application than just filtering out digits.
Anyone have a fancy Scala one-liner for this that is more direct?
You can use filter, treating the string as a character sequence and testing the character with isDigit:
"+72 (93) 2342-7772".filter(_.isDigit) // res0: String = 729323427772
You can use replaceAll and Regex.
"+72 (93) 2342-7772".replaceAll("[^0-9]", "") // res1: String = 729323427772
Another approach, define the collection of valid characters, in this case
val d = '0' to '9'
and so for val a = "+72 (93) 2342-7772", filter on collection inclusion for instance with either of these,
for (c <- a if d.contains(c)) yield c
a.filter(d.contains)
a.collect{ case c if d.contains(c) => c }