My code retrieves the data from various resources .
And output will be like below
UNY4/4/2010
hds04/5/2010
saths04/22/2013
But I want the output like this
4/4/2010
4/5/2010
04/22/2013
Is there any way to do this ?
You need to use a regular expression that finds all uppercase and lowercase characters and replaces them with a blank, like this:
Dim rgx As New Regex("[a-zA-Z]")
str = rgx.Replace(str, String.Empty)
An alternate solution is to look for the first numeric digit, then discard all text before that.
Function GetDate(data As String) As Date
Dim indexFirstNum As Integer = data.IndexOfAny("0123456789".ToCharArray())
Dim datePortion As String = data.Substring(indexFirstNum)
Return Date.Parse(datePortion)
End Function
Related
I have a string ABC(N9KGRTLMN9(0J)M3.
I want to return the character after GRTLM which is N. Thanks.
Look at the System.Text.RegularExpressions namespace, and create a RegEx object with this expression:
GRTLM(.)
Then you will be able to check the Matches for the expression to find your character. Depending on what you know about that string, you may be able to narrow things even further. For example:
GRTLM([A-Za-z])
or
GRTLM([A-Z])
If you don't want to use regular expressions (for any reason), here's an alternative:
Private Function ReturnCharAfter(Source As String, after As String) As Char
Dim i As Integer = Source.IndexOf(after)
If i < 0 Then Return Nothing
Return Source(i + after.Length)
End Function
usage:
Dim N As Char = ReturnCharAfter("ABC(N9KGRTLMN9(0J)M3.", "GRTLM")
You could use String.Split() to get the N
Dim input = "ABC(N9KGRTLMN9(0J)M3"
Dim s = "GRTLM"
Dim n = input.Split({s}, StringSplitOptions.RemoveEmptyEntries)(1)(0)
It splits the string into substrings using GRTLM as a delimiter, then returns the first character of the second array item.
Or to get the index of N
Dim i = input.Split({s}, StringSplitOptions.RemoveEmptyEntries)(0).Length + s.Length
It splits the string and returns the length of the first array item plus the length of the delimiter string.
But perhaps the simplest way to do it is using String.IndexOf()
Dim n = input(input.IndexOf(s) + s.Length)
Dim i = input.IndexOf(s) + s.Length
I have a file name like below:
sub_fa__hotchkis_type1a__180310__PUO4x4__180813
I want to separate it with double underscores "__" and using this code:
Dim MdlNameArr() As String = Path.GetFileNameWithoutExtension(strProjMdlName).Split(New Char() {"__"}, StringSplitOptions.RemoveEmptyEntries)
myTool.Label9.Text = MdlNameArr(1).ToString
I expect the result will be "hotchkis_type1a" but it returns "fa".
It doesnt recognize single underscore "_".
Is there any method to use it properly?
You need to split on a string rather than just a character, so if we look at the available overloads for String.Split, we find the nearest one to that is String.Split(string(), options) which takes an array of strings as the separators and requires the inclusion of StringSplitOptions like this:
Dim s = "sub_fa__hotchkis_type1a__180310__PUO4x4__180813"
Dim separators() As String = {"__"}
Dim parts = s.Split(separators, StringSplitOptions.None)
If parts.Length >= 2 Then
Console.WriteLine(parts(1))
Else
Console.WriteLine("Not enough parts found.")
End If
Outputs:
hotchkis_type1a
I have many strings that have numbers at the end. The numbers can be of any size, for example:
myvar123
mysecondvar3
mythirdvar219107
The strings can have numbers even inside the name, not only at the end.
for example:
my2varable123
some123variable9480395
I would need to replace any number at the END with a placeholder. (NOT those inside the varname.)
for example:
my2varable123 should become: my2variable%placeholder%
some123variable9480395 should become: some123variable%placeholder%
The only way that comes to my mind is to go through the string using .right() and remove the char if it is numeric until I find the first non numeric char. Then in the end append the placeholder, but it looks like a lot of work for a rather simply problem.
Is there a better way to do this?
You could use a Regex for this.
Dim str As String = "some123variable9480395"
Dim pattern As String = "\d+$"
Dim replacement As String = "%placeholder%"
Dim rgx As Regex = New Regex(pattern)
Dim result As String = rgx.Replace(str, replacement)
I need a bit help.
I want to split decimal and character in a string.
Eg : 0.5Lg ---> 0.5 separate and Lg separate.
0.22Ldd --->0.22 separate and Ldd Separate
I tried Following:
Dim input As String = "0.22Ldd"
Dim pattern As String = "[^0-9\.]+"
Dim substrings() As String = Regex.Split(input, pattern)
TextBox11.Text = substrings(0)
This gives output of 0.22 but how to extract Ldd part?
I tried replacing pattern with "\D+" and "\d+" and "\W+" and [a-zA-Z]+ etc etc after googling but no luck.
Can somebody help. I have googled a lot either output comes along with dot or along with number.
You can use this LINQ approach:
Dim input As String = "0.22Ldd"
Dim numericPart = input.TakeWhile(Function(c) Char.IsPunctuation(c) orelse Char.IsDigit(c)).ToArray()
Dim number as Decimal
Dim validNumber = Decimal.TryParse(new String(numericPart), NumberStyles.Any, NumberFormatInfo.InvariantInfo, number)
Dim letterPart As String = input.Substring(numericPart.Length)
if you want to split them with regex you can try pattern below. Regex.Split gives you matching patterns so you should check for number pattern and text pattern in single regex expression.
Dim pattern As String = "([a-zA-Z]*)([0-9]*[.]?[0-9]+)"
I want to filter out other characters from a string as well as split the remaining numbers with periods.
This is my string: major.number=9minor.number=10revision.number=0build.number=804
and this is the expected output: 9.10.0.804
Any suggestions?
As to my comment, if your text is going to be constant you can use String.Split to remove the text and String.Join to add your deliminators. Quick example using your string.
Sub Main()
Dim value As String = "major.number=9minor.number=10revision.number=0build.number=804"
Dim seperator() As String = {"major.number=", "minor.number=", "revision.number=", "build.number="}
Console.WriteLine(String.Join(".", value.Split(seperator, StringSplitOptions.RemoveEmptyEntries)))
Console.ReadLine()
End Sub
If your string does not always follow a specific pattern, you could use Regex.Replace:
Sub Main()
Dim value as String = "major.number=9minor.number=10revision.number=0build.number=804"
Dim version as String = Regex.Replace(value, "\D*(\d+)\D*", "$1.") ' Run the regex
version = version.Substring(0, version.Length - 1) ' Trim the last dot
End
Note you should Imports System.Text.RegularExpressions.