How do I check if a string is a valid integer? - stanza

Let's say I have
val s = "123"
val r = "a123h"
How do I check if s can be resolved to a valid integer?

The signature of to-int in the package core is
public lostanza defn to-int (s:ref<String>) -> ref<False|Int>
So you need :
defn is-int? (s: String) -> True|False :
to-int(s) is Int
N.B.: to-int accepts hexadecimal 0x..., octal 0o... and binary 0b... string representations in addition to decimal ones.

Related

convert string to list of int in kotlin

I have a string = "1337" and I want to convert it to a list of Int, I tried to get every element in the string and convert it to Int like this string[0].toInt but I didn't get the number I get the Ascii value, I can do it with this Character.getNumericValue(number), How I do it without using a built it function? with good complexity?
What do you mean "without using a built in function"?
string[0].toInt gives you the ASCII value of the character because the fun get(index: Int) on String has a return type of Char, and a Char behaves closer to a Number than a String. "0".toInt() == 0 will yield true, but '0'.toInt() == 0 will yield false. The difference being the first one is a string and the second is a character.
A oneliner
string.split("").filterNot { it.isBlank() }.map { it.toInt() }
Explanation: split("") will take the string and give you a list of every character as a string, however, it will give you an empty string at the beginning, which is why we have filterNot { it.isBlank() }, we then can use map to transform every string in our list to Int
If you want something less functional and more imperative that doesn't make use of functions to convert there is this
val ints = mutableListOf<Int>() //make a list to store the values in
for (c: Char in "1234") { //go through all of the characters in the string
val numericValue = c - '0' //subtract the character '0' from the character we are looking at
ints.add(numericValue) //add the Int to the list
}
The reason why c - '0' works is because the ASCII values for the digits are all in numerical order starting with 0, and when we subtract one character from another, we get the difference between their ASCII values.
This will give you some funky results if you give it a string that doesn't have only digits in it, but it will not throw any exceptions.
As in Java and by converting Char to Int you get the ascii equivalence.
You can instead:
val values = "1337".map { it.toString().toInt() }
println(values[0]) // 1
println(values[1]) // 3
// ...
Maybe like this? No-digits are filtered out. The digits are then converted into integers:
val string = "1337"
val xs = string.filter{ it.isDigit() }.map{ it.digitToInt() }
Requires Kotlin 1.4.30 or higher and this option:
#OptIn(ExperimentalStdlibApi::class)

Converting String into Primitive Types in java...Possible?

I want to know that is it possible to convert String into char, int,float,double and other primitive types
If yes then How.. If no then why..we can't do it.
In primitive type we can convert any primitive into any other using Type Casting..so Is there Something to do so with the Strings. As we know that we can convert any primitive type into String ,so is it possible to convert String into Primitive types..
Of course it is possible in java. As you know String is a class in java. this String class defines a method toCharArray() which is used to convert a String object to character array. Its return type is array of characters.
String str = "java";
char arr[];
arr = str.toCharArray();
Just use:
String str = "myString";
char[] charArray = str.toCharArray();
Finally found some really easy and working way..to convert String into Primitive Types...
We Can use wrapper class for conversion
But we have pay extra attention converting String into Primitive Types because we can only convert String into the requires primitive type if primitive type actually supports the data....i.e we can't convert a String into int if it's no(0 to 9),
any other data will throw runtime exception...Same will be applied to all other Primitive Types
//String to primitive types
int iVal = Integer.parseInt(str);
long lVal = Long.parseLong(str);
float fVal = Float.parseFloat(str);
double dVal = Double.parseDouble(str);

How to convert Char to String in Julia?

In julia, Char and String are not comparable.
julia> 'a' == "a"
false
How can I convert a Char value to a String value?
I have tried the following functions, but none of them work.
julia> convert(String, 'a')
ERROR: MethodError: Cannot `convert` an object of type Char to an object of type String
julia> String('a')
ERROR: MethodError: Cannot `convert` an object of type Char to an object of type String
julia> parse(String, 'a')
ERROR: MethodError: no method matching parse(::Type{String}, ::Char)
The way is
string(c)
e.g.
julia> string('🍕')
"🍕"
The string function works to turn anything into its string representation, in the same way it would be printed. Indeed
help?> string
search: string String stringmime Cstring Cwstring RevString readstring
string(xs...)
Create a string from any values using the print function.
julia> string("a", 1, true)
"a1true"
Just wanted to add that the uppercase String constructor can be successfully used on Vector{Char}, just not on a single char, for which you'd use the lowercase string function. The difference between these two really got me confused.
In summary:
To create a char vector from a string: a = Vector{Char}("abcd")
To create a string from a char vector: s = String(['a', 'b', 'c', 'd'])
To create a string from a single char: cs = string('a')

How to convert string to integer in Javascript(Rhino) Programming Language?

I have one variable which contains integer values in string format as given below:
strValue = "123"
I want to convert this string value to integer type so it will be helpful for mathematical operation.
How can I do that in Javascript(Rhino) Language?
You can use the parseInt function that recieves a string and returns int.
Example:
var string = "123"
var number = parseInt(string)
print(number * 2)

Is it possible to compare two characters in Processing?

I am a novice programmer and I am trying to compare two characters from different strings, such that I can give an arbitrary index from each string and check to see if they match. From the processing website it seems that you can compare two strings, which I have done, but when I try to do so with characters it seems that the arguments (char,char) are not applicable. Can someone tell me where I am going wrong? Thanks.
You can use String's charAt() method/function to get character from each string at the desired index, then simply compare:
String s1 = ":)";
String s2 = ";)";
void setup(){
println(CompareCharAt(s1,s2,0));
println(CompareCharAt(s1,s2,1));
}
boolean CompareCharAt(String str1,String str2,int index){
return s1.charAt(index) == s2.charAt(index);
}
Note that when you're comparing strings == doesn't help, you need to use String's equal()
String s1 = ":)";
String s2 = ";)";
println(s1.equals(s2));
println(s1.equals(":)"));
Also, if data comes from external sources, it's usually a good idea to compare both strings at using the same case:
println("MyString".equals("myString"));
println("MyString".toLowerCase().equals("myString".toLowerCase()));
maybe you can pass the argument after converting(typecasting) the char to string.
(string(char),string(char))
Yep. Just use == as it gets interpreted as a char datatype.
This is assuming you've split the char from the String...
char a = 'a';
char b = 'a';
if(a == b) {
// etc
}
As mentioned above, use .equals() for String comparison.
String a = "a";
String b = "a";
if(a.equals(b)) {
// etc
}
Also, the proper way to cast a char as a String is str() not string()

Resources