Im just wondering if it is possible to put a variable in a pattern match in Lua. Like something similar to the following:
var = "hello"
pattern = string.match(datasource, "(var)%s(a%+)")
The reason I need to do this is because the variable "var" will change periodically. (it will be in a loop)
Cheers in advance
Lua doesn't handle string interpolation inside of the quotes. Instead, you'll need to concatenate the parts with the var as a var reference and the rest as quote strings.
"("..var..")%s(a%+)" starts with a "(" as a string literal, concatenates the variable, then finishes off the rest of the string with a string literal.
Use "("..var..")%s(a%+)" instead.
I needed the same thing I think, a variable in a pattern match, but the above solution didn't work for me. I'm posting my solution in case it helps someone, didn't find anything else on the net like it.
I read a ': ' delimited file (name: tel) and want to search by name in the file and have the name and telephone number as answer.
local FileToSearch = h:read'*a' -- Read all the file
var = io.read() -- ask the name
string.gmatch(FileToSearch, ''..var..': '..'%d+') -- search for name, concatenate with number
Related
Let's take a string var str = "Hello Kotlin". I want to copy first 5 character of str to another variable strHello. I was wondering is there any function of doing this or I have to apply a loop and copy characters one by one.
As Tim commented, there's a substring() method which does exactly this, so you can simply do:
val strHello = str.substring(0, 5)
(The first parameter is the 0-based index of the first character to take; and the second is the index of the character to stop before.)
There are many, many methods available on most of the common types. If you're using an IDE such as IDEA or Eclipse, you should see a list of them pop up after you type str.. (That's one of many good reasons for using an IDE.) Or check the official documentation.
Please use the string.take(n) utility.
More details at
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/take.html
I was using substring in my project, but it gave an exception when the length of the string was smaller than the second index of substring.
val name1 = "This is a very very long name"
// To copy to another string
val name2 = name1.take(5)
println(name1.substring(0..5))
println(name1.substring(0..50)) // Gives EXCEPTION
println(name1.take(5))
println(name1.take(50)) // No EXCEPTION
I am working in an ABAP program and I have a question.
For example in C# when we have a String variable: string name; , and we want this to be filled with some data from a textbox but also add some ohter text.
For example:
string name = "Hello: " + textBox1.text;,
And I want to ask you how can I do this in ABAP ??? How to add text plus the text written from a Parameter type C?
CONCATENATE and the concatenate operator && will do it as answered by Jagger and vwegert. To do it with string expressions, you use the below where name is the screen field or whatever that has the name in it (it doesn't need to be a field-symbol):
greeting = |Hello: { <name> }|.
String expressions are extremely useful as they can be used to build up complex values without declaring extra variables - e.g. they can passed as directly as function module or method parameters without first assigning to a local variable.
You can either use the CONCATENATE keyword or -- in newer releases -- string expressions. Be sure to check the online documentation and sample programs available using the transaction ABAPDOCU, it will save you a ton of seemingly basic questions.
The equivalent operator is &&.
So in your case it would be:
name = 'Hello: ' && textBox1->text.
In vba I am openening a table from access with a column that look like the following:
1300nm11-53-0202 0302.SOR
I would like to look for the very first time "nm" is found in the string and write everything that is before that into a variable "strGolfLengte" (so In this case strGolflengte would be "1300")
NB:
I can't be sure that there won't be several nm's in the string, I just want to look for the first time they are found.
NB2:
The string before nm could be "n" characters, in all cases, I want the full lenght (n) of the string written in strGolflengte
I would use the `instr()' function like this:
strGolfLengte = left(myLine,instr(1,myLine,"nm",1))
I think it is the easiest way to do this:
strGolfLengte = Split(myLine,"nm")(0)
I have this string:
201057&channelTitle=null_JS
I want to be able to cut out the '201057' and make it a new variable. But I don't always know how long the digits will be, so can I somehow use the '&' as a reference?\
myDigits substring(0, position of &)?
Thanks
Sure, you can split the string along the &.
String s = "201057&channelTitle=null_JS";
String[] parts = s.split("&");
String newVar = parts[0];
The expected result here is
parts[0] = "201057";
parts[1] = "channelTitle=null_JS";
In production code you chould check of course the length of the parts array, in case no "&" was present.
Several programming languages also support the useful inverse operation
String s2 = parts.join("&"); // should have same value like s
Alas this one is not part of the Java standard libs, but e.g. Apache Commons Lang features it.
Always read the API first. There is an indexOf method in String that will return you the first index of the character/String you gave it.
You can use myDigits.substring(0, myDigits.indexOf('&');
However, if you want to get all of the arguments in the query separately, then you should use mvw's answer.
I am trying to perform a Substring function on a image filename.
The name format is in "images.png".
I tried using Substring it only allow me to indicate the first character till the "n" character to perform the function.
Such that SubString(1,6).
But what I want is to get any character before the ..
For example "images.png":
After the Substring function I should get "images".
You can use LastIndexOf in conjunction with Substring:
myString.Substring(0, myString.LastIndexOf('.'))
Though the Path class has a method that will do this in a strongly typed manner, whether the passed in path has directories or not:
Path.GetFileNameWithoutExtension("images.png")
How about using the Path class.
Path.GetFileNameWithoutExtension("filename.png");
In general for such string manipulations you can use:
mystring.Split("."c)(0)
But specifically for getting a filename without extension, it's best to use this method:
System.IO.Path.GetFileNameWithoutExtension
Dim fileName As String = "images.png"
fileName = IO.Path.GetFileNameWithoutExtension(fileName)
Debug.WriteLine(fileName)
http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx
string s = "images.png";
Console.WriteLine(s.Substring(0, s.IndexOf(".")));