How to remove a character from at string at certain position from the end? - vb.net

I have a string, for example:
Dim str as string = xxxxxxxxxxxxxxxxxxxx£xxx£xxxx**£**xxxxxxxxxx
I want to remove £ surrounded from * which is always at a certain position (11th for instance) from the end. The whole string is a long one, always change in size and cannot be counted from the start. I cannot use Replace as well, there may be same characters at other positions that I do not wish to remove.
Solution:
Dim rst As String = str.Remove(str.Length - 11, 1)

Edit: Whoops, I dunno what I was thinking on that first part.
The correct version of the first part would be:
str = str.Substring(0, str.Len -13) + str.Substring(str.Len-11);
There also may be an overload for the String.Delete function that allows you to use a negative number to represent the number of characters from the end of the string -- I know that the C# equivalent does.

If its always going to be the 11th character from the end you can do this...
Dim strTargetString As String = "xxxYxxxxxxxxxx"
Dim strTargetString2 As String = "xxxxxxxYxxxxxxxxxx"
Dim strResult As String = Mid(strTargetString, 1, (Len(strTargetString) - 11)) & Microsoft.VisualBasic.Right(strTargetString, 10)
Dim strResult2 As String = Mid(strTargetString2, 1, (Len(strTargetString2) - 11)) & Microsoft.VisualBasic.Right(strTargetString, 10)
Note that String.SubString is a more modern approach than Mid, but I use it out of preference and example.

This is fairly straightforward with a regular expression replacement operation using look-ahead:
Dim str as String = "xxxxxxxxxxxxxxxxxxxx£xxx£xxxx£xxxxxxxxxx"
Dim str2 as String = Regex.Replace(str, "£(?=.{10}$)", String.Empty)
This will target a single character followed by any ten characters then the end of the string and replace it with the String.Empty value (or just "" if you'd prefer).

Related

Get the character after a combination of characters in a string

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

How to split on a string instead of a character?

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

Vb.net, replace any number at the end of a string with a placeholder

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)

How to split decimals and charecter from a string

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]+)"

Last but one Char in vb.net String

How do I find last but one character in a vbstring
for e.g. In the string V1245-12V0 I want to return V
Don't use substring to get just one character
Dim MyString As String = "V1245-12V0"
Dim MyChar As Char = MyString(MyString.Length - 2)
Sorry it's been a while since I did VB so this may not be perfect (and is probably a mixture of C# and VB) but you get the idea:
Dim s = "V1245-12V0"
Dim lastButOneLetter = String.Empty
If s.Length > 1 Then
'Can only get the last-but-one letter from a string that is minimum 2 characters
lastButOneLetter = s.Substring(s.Length - 2, 1)
Else
'do something if string is less than 2 characters
End If
EDIT: fixed to be compilable VB.NET code.
Dim secondToLastChar As Char
secondToLastChar = Microsoft.VisualBasic.Strings.GetChar(mystring, mystring.Length - 2)
http://msdn.microsoft.com/en-us/library/4dhfexk4(VS.80).aspx
Or just remember that any string is an array of chars;
secondToLastChar = mystring(mystring.Length - 2)
If you want to get the last alpha-character in a string you could use a LINQ query such as (C#):
var d = from c in myString.ToCharArray().Reverse()
where Char.IsLetter(c)
select c;
return d.First();
string.Substring(string.Length - 2, 1);
Was it difficult?
dim mychar as string
dim yourstring as string
yourstring="V1245-12V0"
mychar=yourstring.Substring(yourstring.Length - 2, 1)
Use the Substring on the string s which contains 'V1245-12V0'
s.Substring(s.Length - 2, 1);
Here's a VB solution:
Dim text = "V1245-12V0"
Dim v = Left(Right(text, 2), 1)
You do not need to check the length of text, except for your semantics as to what you want to happen for empty (and Nothing) and single character strings.
You can have your own functions like
Function Left(ByVal str as string, byval index as integer) As String
Left=str.Substring(0,index);
End Function
Function Right(ByVal str as string, byval index as integer) As String
Right=str.Substring(str.Length-index)
End Function
And use them to get what you need.