Lua: String to name? - string

is it possible to convert a string into a name of a table entry?
just like
string = "test"
y = {test = 123}
print y.string
So if string is "test" it would print y.test if there is such a entry
Hope you understand my question!
Thanks in advance...

print(y[string])
Be careful with using names such as string for variables, especially globals. You've just overwritten the string table.

Related

How to replace a string in javascript

Here is my data
data = "67.45"
want to remove the double quotes and wanted my out to be as
data = 67.45
What is the best way to achieve
Thanks in advance
var data = "67.45"
console.log(parseFloat(data))
data is having string value. if you want to convert string to float.
you just need to use parseFloat.
var data = "67.45"
parseFloat(data)
you will get float value
you can do this:
var ret = data.replace('"','');
console.log(ret); //should show your string with out quotes
or you can use this:
data.replace(/"/g,'');

Splitting and parsing a String value in a pre-defined pattern in Groovy

Can someone please help me with how can a string value be converted into a predefined format by splitting the string?
For example:
If the
Stringvalue = "20210819"
needs to be converted as "08/19/2021"
(Splitting first four and then next two and again next two)
Thanks,
Thanks, #DaveNewton, I was able to do that as you suggested.
Stringvalue = "20210819"
valuedate=Stringvalue
valueyear=(valuedate[0..3]);
valuemonth=(valuedate[4..5]);
valueday=(valuedate[6..7]);
println (valuemonth+"/"+valueday+"/"+valueyear);

Format string with list in Python3

Lets say I have string: s = '{1} goes to {0}'
And I want to format this string with list: l = ['Hollywood', 'Frankie']
I cannot modify string and list both. Is there way to write simple piece of code to handle this case?
PS. I know about question "Python Format String with List", but it is not what Im asking.
Use the unpack operator * when passing the list to the format method.
s = '{1} goes to {0}'
l = ['Hollywood', 'Frankie']
print(s.format(*l))
This outputs:
Frankie goes to Hollywood

Swift2 - Split String to individual characters

I am trying to split a string into individual characters.
The string I want to split: let lastName = "Kocsis" so that is returns something like: ["K","o","c","s","i","s"]
So far I have tried:
var name = lastName.componentsSeparatedByString("")
This returns the original string
name = lastName.characters.split{$0 == ""}.map(String.init)
This gives me an error: Missing argument for parameter #1 in call. So basically it does't accept "" as an argument.
name = Array(lastName)
This does't work in Swift2
name = Array(arrayLiteral: lastName)
This doesn't do anything.
How should I do this? Is There a simple solution?
Yes, there is a simple solution
let lastName = "Kocsis"
let name = Array(lastName.characters)
The creation of a new array is necessary because characters returns String.CharacterView, not [String]

Is there any way to retrieve a appended int value to a String in javaScript?

I am currently working on a project that dynamically displays DB content into table.
To edit the table contents i am want to use the dynamically created "string"+id value.
Is there any way to retrieve the appended int value from the whole string in javaScript?
Any suggestions would be appreciative...
Thanks!!!
If you know that the string part is only going to consist of letters or non-numeric characters, you could use a regular expression:
var str = "something123"
var id = str.replace(/^[^\d]+/i, "");
If it can consist of numbers as well, then things get complicated unless you can ensure that string always ends with a non-numeric character. In which case, you can do something like this:
var str = "something123"
var id = str.match(/\d+$/) ? str.match(/\d+$/)[0] : "";
(''+string.match(/\d+/) || '')
Explanation: match all digits in the variable string, and make a string from it (''+).
If there is no match, it would return null, but thanks to || '', it will always be a string.
You might try using the regex:
/\d+$/
to retrieve the appended number

Resources