what is the equivalent of UBound and Split(vb.net) in c#? - vb.net-to-c#

i have codes like this in vb.net
If UBound(aTicketEntry) < 17
and
aTicketEntry = Split(sBarcodeValidation, "|")
and i try to code them like this but i got error on both them
aTicketEntry = Split(sBarcodeValidation,"|");
and
if ((UBound(aTicketEntry) < 17))
how can i fix this? or what is the equivalent of UBound and Split?

For UBound() you would use .length. For example:
If (aTicketEntry.length < 16) { /*…*/}
Note that I changed the 17 to 16, this is because C#'s length property will index from 0 instead of 1.
For Split() I would just use the String.Split() method, its similar just a change in syntax:
string[] aTicketEntry;
aTicketEntry = sBarcodeValidation.Split("|");
Apologies about the late response I didn't realize this tag was so quiet, I'll make a point to check for responses if you're still working on this.

Related

Remove operations using regex [duplicate]

This question already has answers here:
How to find sum of integers in a string using JavaScript
(3 answers)
Closed 3 years ago.
I am getting a string back "1+2" and would like to remove the "+" and then add the numbers together.
Is this possible using Regex? So far I have:
let matches = pattern.exec(this.expression);
matches.input.replace(/[^a-zA-Z ]/g, "")
I am now left with two numbers. How would I add together?
"this.a + this.b"
Assuming the string returned only has '+' operation how about:
const sum = str.split('+').reduce((sumSoFar, strNum) => sumSoFar + parseInt(strNum), 0);
You cannot add two numbers using regex.
If what you have is a string of the form "1+2", why not simply split the string on the + symbol, and parseInt the numbers before adding them?
var str = "1+2";
var parts = str.split("+"); //gives us ["1", "2"]
console.log(parseInt(parts[0]) + parseInt(parts[1]));
If you don't always know what the delimiter between the two numbers is going to be you could use regex to get your array of numbers, and then reduce or whatever from there.
var myString = '1+2 and 441 with 9978';
var result = myString.match(/\d+/g).reduce((a,n)=> a+parseInt(n),0);
console.log(result); // 1 + 2 + 441 + 9978 = 10422
*Edit: If you actually want to parse the math operation contained in the string, there are a couple of options. First, if the string is from a trusted source, you could use a Function constructor. But this can be almost as dangerous as using eval, so it should be used with great caution. You should NEVER use this if you are dealing with a string entered by a user through the web page.
var myFormula = '1+2 * 441 - 9978';
var fn = new Function('return ' + myFormula);
var output = fn();
console.log(myFormula, ' = ', output); //1+2 * 441 - 9978 = -9095
A safer (but more difficult) course would be to write your own math parser which would detect math symbols and numbers, but would prevent someone from injecting other random commands that could affect global scope variables and such.

Finding functions with default value in javascript

I am having error when uploading my website on heroku as having default values in function.
Until now I have used default value lots of times and they are in 50000 column javascript file and I cant really find all of them.
So I tried to find all functions with default values in sublime using regex.
I tried this regular expression.
function.*\(.*\=.*\)
But it's finding this kind of string(which is not default value function).
Function(e)) return Q.grep(t, function(t, i) { return !!e.call(t, i, t) !== n })
Sample function to be searched
var abc = function(x , y = 1)
function abc(x , y = 1)
Please help, I am new to regular expression and it's really difficult to find answer.
Try this regex function[a-zA-Z ]*\([a-zA-Z \,]+[a-zA-Z ]*[=]. It will match the functions with default parameters regardless of number of parameters. I hope this helped.
function\([^)]+\)
and take refernce from this:
Regular expression to find all function calls with parameters in VS2010
Try this one function[\s\w]*\([\w\s\d,]*[\w\s]+=[\w\d\s'",]*\). Default value may be string, and also function may have name:
var abc = function some_name (x , y = 1, z = "string", z_z) { var = 1

QString - parsing QString with geoordinates

I work in Qt Creator (Community) 5.5.1. For example, I have
string="44° 36' 14.2\" N, 33° 30' 58.6\" E, 0m"
of QString. I know, that I must parse it, but i don't know how, because I have never faced with the problem like it. From our string I want to get some other smaller strings:
cgt = "44"; cmt = "36"; cst = "14.2"
cgg = "33"; cmg = "30"; csg = "58.6"
What must I do for working my programm how I said?
I need real code. Thanks.
The simplest way to start would be string.split(' ') - that would yield the list of the string components that were separated by the space character (' '). If you're sure the string will always be formatted exactly like this, you can first remove all the special characters (° and so on).
Then analyze the resulting QStringList. Again, if the format is fixed, you can check that the number of list items matches the expected number, and then get degrees as list[0], minutes as ``list[1]` and so on.
Another alternative would be to use QRegExp for parsing the string (splitting it into substrings based on regex), but I find it too complicated for use cases where split works just as well.
"I need code" is not the kind of question you should be asking, SO is about "gimme knowledge" not about "do my work" questions. A good question should demonstrate your effort to solve the problem, so people can tell you what you are doing wrong. Not only does your question lack any such effort, but you didn't expend any even when Devopia did half of the work for you. Keep that in mind for your future questions.
struct G {
double cgt, cmt, cst, cgg, cmg, csg;
};
G parse(QString s) {
QStringList list = s.split(QRegExp("[^0-9.]"), QString::SkipEmptyParts);
G g;
g.cgt = list.at(0).toDouble();
g.cmt = list.at(1).toDouble();
g.cst = list.at(2).toDouble();
g.cgg = list.at(3).toDouble();
g.cmg = list.at(4).toDouble();
g.csg = list.at(5).toDouble();
return g;
}

Regex Split after 20 characters

I have a fixed width text file where each field is given 20 characters total. Usually only 5 characters are used and then there is trailing whitespace. I'd like to use the Split function to extract the data, rather than the Match function. Can someone help me with a regex for this? Thanks in advance.
I would do this with string manipulation, rather than regex. If you're using JavaScript:
var results = [];
for (i = 0; i < input.length; i += 20) {
results.push(input.substring(i, i + 20));
}
Or to trim the whitespace:
var results = [];
for (i = 0; i < input.length; i += 20) {
results.push(input.substring(i, i + 20).replace(/^\s+|\s+$/g, ''));
}
If you must use regex, it should just be something like .{20}.
Split on whitespaces and get the first returned element. This is under the assumption that you do not have whitespaces within the actual data.
cheers
If you must:
^(.{20})(.{20})(.{20})$ // repeat the part in parentheses for each field
You still need to trim each field to remove trailing whitespace.
It seems simpler to use substr() or your languages equivalent. Or in PHP you could use str_split($string, 20).

VB.Net Matching and replacing the contents of multiple overlapping sets of brackets in a string

I am using vb.net to parse my own basic scripting language, sample below. I am a bit stuck trying to deal with the 2 separate types of nested brackets.
Assuming name = Sam
Assuming timeFormat = hh:mm:ss
Assuming time() is a function that takes a format string but
has a default value and returns a string.
Hello [[name]], the time is [[time(hh:mm:ss)]].
Result: Hello Sam, the time is 19:54:32.
The full time is [[time()]].
Result: The full time is 05/06/2011 19:54:32.
The time in the format of your choice is [[time([[timeFormat]])]].
Result: The time in the format of your choice is 19:54:32.
I could in theory change the syntax of the script completely but I would rather not. It is designed like this to enable strings without quotes because it will be included in an XML file and quotes in that context were getting messy and very prone to errors and readability issues. If this fails I could redesign using something other than quotes to mark out strings but I would rather use this method.
Preferably, unless there is some other way I am not aware of, I would like to do this using regex. I am aware that the standard regex is not really capable of this but I believe this is possible using MatchEvaluators in vb.net and some form of recursion based replacing. However I have not been able to get my head around it for the last day or so, possibly because it is hugely difficult, possibly because I am ill, or possibly because I am plain thick.
I do have the following regex for parts of it.
Detecting the parentheses: (\w*?)\((.*?)\)(?=[^\(+\)]*(\(|$))
Detecting the square brackets: \[\[(.*?)\]\](?=[^\[+\]]*(\[\[|$))
I would really appreciate some help with this as it is holding the rest of my project back at the moment. And sorry if I have babbled on too much or not put enough detail, this is my first question on here.
Here's a little sample which might help you iterate through several matches/groups/captures. I realize that I am posting C# code, but it would be easy for you to convert that into VB.Net
//these two may be passed in as parameters:
string tosearch;//the string you are searching through
string regex;//your pattern to match
//...
Match m;
CaptureCollection cc;
GroupCollection gc;
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
m = r.Match(tosearch);
gc = m.Groups;
Debug.WriteLine("Number of groups found = " + gc.Count.ToString());
// Loop through each group.
for (int i = 0; i < gc.Count; i++)
{
cc = gc[i].Captures;
counter = cc.Count;
int grpnum = i + 1;
Debug.WriteLine("Scanning group: " + grpnum.ToString() );
// Print number of captures in this group.
Debug.WriteLine(" Captures count = " + counter.ToString());
if (cc.Count >= 1)
{
foreach (Capture cap in cc)
{
Debug.WriteLine(string.format(" Capture found: {0}", cap.ToString()));
}
}
}
Here is a slightly simplified version of the code I wrote for this. Thanks for the help everyone and sorry I forgot to post this before. If you have any questions or anything feel free to ask.
Function processString(ByVal scriptString As String)
' Functions
Dim pattern As String = "\[\[((\w+?)\((.*?)\))(?=[^\(+\)]*(\(|$))\]\]"
scriptString = Regex.Replace(scriptString, pattern, New MatchEvaluator(Function(match) processFunction(match)))
' Variables
pattern = "\[\[([A-Za-z0-9+_]+)\]\]"
scriptString = Regex.Replace(scriptString, pattern, New MatchEvaluator(Function(match) processVariable(match)))
Return scriptString
End Function
Function processFunction(ByVal match As Match)
Dim nameString As String = match.Groups(2).Value
Dim paramString As String = match.Groups(3).Value
paramString = processString(paramString)
Select Case nameString
Case "time"
Return getLocalValueTime(paramString)
Case "math"
Return getLocalValueMath(paramString)
End Select
Return ""
End Function
Function processVariable(ByVal match As Match)
Try
Return moduleDictionary("properties")("vars")(match.Groups(1).Value)
Catch ex As Exception
End Try
End Function