How to Trim right and left a String in VB .net - vb.net

I want to take the value of
T.GS.+0.220kg
but I don't know how to remove the string.
I just want to take numbers from the weight.
like 0.220
Can someone help me ?

You can make use of the Regular Expressions to extract a decimal value from basically any string. First you'd need to import the library:
Imports System.Text.RegularExpressions
Then using this will return just the decimal value:
Regex.Match("T.GS.+0.220kg", "\d+.\d+").Value
This particular expression looks for a digit or digits, followed by a point (dot), followed by another number of digits, so the previous points (in between T and G for example) aren't included.
This returns exactly 0.220, you can then replace the string with any string variable and assign this expression as needed.

If you havn't worked with regular expressions before and want somthing that looks a little nicer. You could use the string.split method.
dim input as string = "T.GS.+0.220kg"
input = input.split("+")(1) ' which will grab the "0.220kg"
input = input.substring(0, input.length - 2) ' then filter off the last 2 chars
In english:
split the string into 2 seperate pieces grabing the part to the right of the first '+' symbol.
Then remove the last 2 chars from the end.

Related

using Regex get substring between underscore 2 and underscore 3 of string, vb.net

I have a string like: Title Name_2021-04-13_A+B+C_Division.txt. I need to extract the A+B+C. The A+B+C may be other letters. I believe that using Regex would be the simplest way to do this. In other words I need to get the substring between underscore 2 and underscore 3 of string. All of my code is written in vb.net. I have tried:
boatClass = Regex.Match(myFile, "(?<=_)(.*)(?=_)").ToString
I know this is not right but I think it is close. What do I need to add or change?
The regex code that will extract a substring between the second and third underscore of a string is:
(?:[^_]+_){2}([^_]+)
However, I chose to use the split function:
myString.Split("_"c)(2)

How can I add a string character based on a position in OpenRefine?

I have a column in Openrefine, which I would like to add a character string in each of its rows, based on the position in the string.
For example:
I have an 8th character number string: 85285296 and would like to add "-" at the fourth place: "8528-5296".
Anyone can help me find the specific function in OpenRefine?
Thanks
Tzipy
The simplest approach is to just use the expression language's built-in string indexing and concatenation:
value[0,4]+'-'+value[4,8]
or more generally, if you don't know that your value is exactly 8 characters long:
value[0,4]+'-'+value[4,999]
Possible solution (not sure if it's the most straightforward):
value.replace(/(\d{4})(.+)/, "$1-$2")
This means : if $1 represents the content of the first parenthesis/group in the regular expression before and $2 the content of the second one, replaces each value in the column with $1-$2.
Some other options:
value.splitByLengths(4,4).join("-")
value.match(/(\d{4})(\d{4})/).join("-")
value.substring(0,4)+"-"+value.substring(4,8)
I think 'splitByLengths' is the neatest, but I might use 'match' instead because it fails with an error if your starting string isn't 8 digits - which means you don't accidentally process data that doesn't conform to your assumption of what data is in the column - but you could use a facet/filter to check this with any of the others

How Do I get this Split Function to Work? (VB.NET)

So, I made a program that for the most part, converts numbers to letters. My problem before was it was converting each individual digit instead of each number e.g. (1-0-1 instead of 101). Someone suggested that I use the Split function:
Dim numbers As String() = DTB.Split(" ")
So now it's reading the number all the way through being that it will only the split if there's a space in between. My problem now is that it's translating for example: "[102, 103, 104]" as "[102", "103" and "104]" because it will only split if there's a space between. Obviously, you can't convert "[102" or "104]" because they aren't actual numbers.
Does anyone have a solution on what I should do to get this to convert no matter the spacing? Would Regex be the way to go?
use a regular expression with \d+ it will match numbers
so
12234abcsdf23434
will return two matches
12234
23434

how to cut part of string

How to cut part from this string...
"abb.c.d+de.ee+f.xxx+qaa.+.,,s,"
... where i know position by this:
Result is always between "." (left side of result) and "+" (right side).
I know number of "." from left side and number of "+" from right side, to delimit resulting string.
Problem is right side, cause i need to count "+" from end.
Say...
from left side: begining is at 4th "."
( this is easy ), result is =
"xxx+qaa.+.,,s,"
from right side: end is at second "+" from end!
"xxx[here]+qaa.+.,,s,"
result is =
"xxx"
I try to do this myself with .substring and .indexOf, but with no success...
Any ideas? thanks
You could use the StrReverse function to reverse the character sequence and then count + from the left (using the same method as counting the .).
To find the start of the substring, loop through the string from the left. Count the number of .s you have seen and stop when you've hit the number you want. Store the index in some variable like start.
Similarly to find the end of the substring, loop from the right and count +s.
You can solve this problem using Regex:
Dim r As New Regex("^(.*\.){4}(?<value>.*)(\+.*){2}$")
Dim m As Match = r.Match("abb.c.d+de.ee+f.xxx+qaa.+.,,s,")
Dim result As String = m.Result("${value}")
Explanation
^ Indicates the beginning of the string
(.*\.){4} This means any character (.) repeated any number of times (*) followed by a period (\.). The period has to be escaped with the backslash because otherwise the period would be the any-character wildcard. The .*\. is enclosed in (){4} to say that pattern must repeat four times.
(?<value>.*) This specifies the placeholder for the text we are after. value is the name we are assigning to it. The .* specifies that the value is any number of any characters.
(\+.*){2} This means a plus character (has to be escaped) followed by any number of any characters, repeated twice.
$ Indicates the end of the string

Check if character field contains only digits

I read data from a excel file.
The cols of internal table all are char128, there are 2 cols contain only digital with decimal point. So I need to check the fields only contain digital or digital with decimal point.
The function module NUMERIC_CHECK, just can check only digital, if the digital with decimal point it will be useless.
You may use CO (contains only):
IF value CO '1234567890.'.
"OK
ELSE.
"Error"
ENDIF.
Maybe you need also a space in your IF _ CO-statement.
This check does not detect multiple decimals points (e.g. 123.45.67.89).
Newer versions of ABAP support regular expressions.
If you have also spaces in your string, you may add them into the CO-value:: IF value CO '1234567890 .'.
You might try to use REGEX. The report DEMO_REGEX_TOY lets you input strings and test regular expressions against them.
Someone more experienced with REGEX in general might be able to make this a little more versatile but here's what I came up with:
-?[0-9]+(\.[0-9]*)?
-? Optionally match the '-' character (allows for negatives or non-negatives
[0-9]+ matches digits (the + makes it match 1 or more)
\.? optionally matches the '.' character (the \ is needed as '.' is an operator)
([0-9]+)? optionally matches any digits after the decimal
if you want to check if the string is a valid decimal, you could use the following module function : 'HRCM_STRING_TO_AMOUNT_CONVERT', which allow you to convert a string to its numeric counterpart, given the string, the decimal separator and the thousand separator.
regards
Another way to check for number:
data: float_value type f.
try.
float_value = <your string value here>.
catch cx_sy_conversion_no_number.
" not a valid number
endtry.