I'm sure this question has been asked before, but I can't find exactly what I'm after.
I have a string which has a string, then a dash, then a number, e.g. "TERM-01" which happens to be the name of an electrical terminal in a switchboard.
I want to increment it to "TERM-02", "TERM-03" etc.
I have come up with this:
TermNo = CStr(Mid(TermNo, 1, InStr(TermNo, "-")) & (CInt(Mid(TermNo, InStr(TermNo, "-") + 1, TermNo.Length - InStr(TermNo, "-")) + 1)))
Which seems to work fairly well, however I need to increment the number including the 0, so 08, 09, 10 instead of 8,9,10.
Any ideas?
You could use the standard Substring method to point to the part where the number starts, convert it to an integer and add your increment. The trick to return the number with the 0 prefix is done using the format specifier D2
Dim TermNo = "TERM-01"
for i = 1 To 15
Dim value = "TERM-" + (Convert.ToInt32( _
TermNo.SubString( _
TermNo.IndexOf("-"))) + i) _
.ToString("D2")
Console.WriteLine(value)
Next
This could also be written using a Regex expression in a more readable way
Dim TermNo = "TERM-01"
for i = 1 To 15
Console.WriteLine(Regex.Replace(TermNo, "\d+", i.ToString("D2")))
Next
If you always have a dash, simply split the string on the dash and deal with the pieces individually.
https://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx
Related
In VB.net (Visual Studio 2015) how can I get the nth string (or number) in a comma-separated list?Say I have a comma-separated list of numbers like so:13,1,6,7,2,12,9,3,5,11,4,8,10How can I get, say, the 5th value in this string, in this case 12?I've looked at the Split function, but it converts a string into an array. I guess I could do that and then get the 5th element of that array, but that seems like a lot to go through just to get the 5th element. Is there a more direct way to do this, or am I pretty much limited to the Split function?
In case you are looking for an alternative method, which is more basic, you can try this:
Module Module1
Sub Main()
Dim a As String = "13,1,6,7,2,12,9,3,5,11,4,8,10"
Dim counter As Integer = 5 'the number you want (in this case, 5th one)
Dim movingcounter As Integer = 0 'how many times we have moved
Dim startofnumber, endofnumber, i As Integer
Dim numberthatIwant As String
Do Until movingcounter = counter
startofnumber = InStr(i + 1, a, ",")
i = startofnumber
movingcounter = movingcounter + 1
Loop
endofnumber = InStr(startofnumber + 1, a, ",")
numberthatIwant = (Mid(a, startofnumber + 1, endofnumber - startofnumber - 1))
Console.WriteLine("The number that I want: " + numberthatIwant)
Console.ReadLine()
End Sub
End Module
Edit: You can make this into a procedure or function if you wish to use it in a larger program, but this code run in console mode will give the output of 12.
The solution provided by Plutonix as a comment to my question is straightforward and exactly what I was looking for, to wit:result = csv.Split(","c)(5)In my case I was incrementing a variable each time my program ran and needed to get the nth character or string after the incremented value. That is, if my program had incremented the variable 5 times, then I needed the string after the 4th comma, which of course, is the 5th string. So my solution was something like this:result = WholeString.Split(","c)(IncrementedVariable)Note that this is a zero-based variable.Thanks, Plutonix.
I have a data import file that has dates on it without a comma (i.e. January 1 2015). Excel won't recognize this as a date, and I need to work with these dates to find other dates (i.e. Date + Length of trip to find the last day of the trip, etc.)
I'm trying to use VBA to accomplish this and have the logic down but I'm encountering an error.
I'm taking the length of the date (January 1 2015), the 5 right characters (2015), the left of the whole length minus the 5 right characters(JANUARY 1), and then combining these variables with a comma inserted: Left(value-5) & ", " & Right(value, 5)
I'm using this code:
'correct date from JANUARY 1 2000 to JANUARY 1, 2000 so excel can recognize as date
LengthTrpDpt = Len(wb1.Sheets("BL Import").Cells(ioi, TrpDepCol)) 'length of date
LengthRightTrpDPt = Right(wb1.Sheets("BL Import").Cells(ioi, TrpDepCol), 5) 'finds right 5 of date " 2015"
NewListedDate = Left(wb1.Sheets("BL Import").Cells(ioi, TrpDepCol), LengthTrpDpt - 5) & ", " & LengthRightTrpDPt
The problem is with the NewListedDate variable. I can change the part LengthTrpDpt - 5 to a number and it works fine. For some reason I can't have an equation here though. I tried perform the equation in a separately saved variable (LengthMath = LengthTrpDpt - 5) and using LengthMath instead, but that doesn't work either.
LengthTrpDpt works fine and MsgBox's the correct number. Any ideas? Thanks!
You should just Split() the date string to get your three parts, then piece them back together however you'd like:
Dim a
a = Split(wb1.Sheets("BL Import").Cells(ioi, TrpDepCol))
NewListedDate = a(0) & " " & a(1) & ", " & a(2)
Use Range.TextToColumns method on the column of text-that-look-like-dates.
with worksheets("Sheet1").columns(1) '<-change this to reflect the actual worksheet and column
.TextToColumns Destination:=.cells(1,1), DataType:=xlFixedWidth, FieldInfo:=Array(0, 3)
end with
The 0 tells T2C that it should just put everything back into the original column. The 3 is the MDY TextFileColumnDataTypes property.
imho, it is ALWAYS better to work with numbers and dates rather than a string representation approximating them.
Perhaps this is a simple solution for most, but I can't get this to work like it should according to syntax.
I have this line of text "Part Number123456Price$50.00"
I want to pull the part number out of it, so I use this function...
str = Mid(str, str.IndexOf("Part Number") + 12, str.IndexOf("Price"))
My results are str = "123456Price$50.0" every time. I know the part number can vary in length so I need a solid solution of pulling this out.
It can be confusing to mix the legacy VB string methods (such as Mid) with the .Net string methods (like IndexOf). The VB methods use 1 as the index of the first character while the .Net methods use 0.
The following code will extract the part number from a string
Dim str As String = "Part Number123456Price$50.00"
Dim iPart As Integer = str.IndexOf("Part Number") + 11
Dim iPrice As Integer = str.IndexOf("Price")
str = str.Substring(iPart, iPrice - iPart).Trim
The Mid() function of Visual Basic is documented as having three arguments: (1) a string, (2) the beginning location in the string, and (3) the number of characters to copy.
So if your string is "Part Number123456Price$50.00" and you want to pull the part number as a series of digits, the "123456" part of the string, using the Mid() function then you need to find the beginning of the part number digit string and to then know the number of digits.
If your string is in the variable str then you can find the offset by something like str.IndexOf("Number") + len("Number") which will provide the offset to after the string "Number".
Next you need to find the number of digits so you would do something like str.IndexOf("Price") to find where the text "Price" begins and then subtract from that offset the offset of where the digits begin.
The result of all of this is you need a bit of code something like the following. I have not tested this source as I am not a VB programmer so it may need a tweak and you might want to put some checks on data validity as well.
Dim TextNumber as String = "Number"
Dim TextPrice as String = "Price"
iOffset = str.IndexOf(TextNumber) + len(TextNumber)
str = Mid(str, iOffset, str.IndexOf(TextPrice) - iOffset)
Alternatively, if Price is always the format $00.00, this will also work.
Dim str as String = "Part Number123456Price$50.00"
str = str.Remove(str.IndexOf("Price"))
I'm trying to get the character at a specified point in a string and I am proficient in Java but am learning VB for a competition tomorrow. I am trying to get the n char in a string..
for example, I have string "12345" and I want the middle character from the string which is '3' in this case. currently I'm trying to use
middle = Nums.Chars(CInt((Nums.Length / 2))+1)
where middle is a String. It gives me a character but sometimes that character isn't even the middle character in the string. The value of middle is the middle number + 1 so I assumed it was the character code of the char ( 0 = 1, 1 = 2, 2 = 3, etc..) but that was just my theory. So I tried to change it to a String by using
middle = Nums.Chars(CInt((Nums.Length / 2))+1) & ""
Alas that still didn't work so now I come here. All criticism is greatly appreciated.
TL;DR I need to find the middle character in a String, in my case the string is always an integer converted to a string.
Try this:
Public Function GetMiddleString(original As String) As String
If original.Length Mod 2 <> 0 AndAlso original.Length >= 3 Then
Return original.Substring(original.Length \ 2 + 1, 1)
Else If orginal.Length Mod 2 = 0 AndAlso original.Length >= 3 Then
Return original.Substring(original.Length \ 2, 1)
End If
Return original
End Function
Note: This assumes that original is not null (Not Nothing).
As far as I know there is Mid() function to get the middle or the index of the middle char.
Can't figure out why this won't work.
I am trying to analyse a string of variable length containing a "." somewhere inside, and then strip off the "." and all characters before it. This is called via a web service.
When debugging, it works fine until it bails out at the last line, below, with the browser message:
"System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
"
Anyone got any idea?
Code1, below, is an input variable passed to the web service from an eform.
Dim CharNo As New Integer
CharNo = Code1.IndexOf(".")
MyCodebookValueStrip.o_Code1 = Code1.Substring(CharNo + 1, (Code1.Length - CharNo))
Your calculation of the lenth of the remaining string is incorrect. You have to subtract one more:
Code1.Substring(CharNo + 1, Code1.Length - CharNo - 1)
You can also just omit the second parameter, and it will get the rest of the string:
Code1.Substring(CharNo + 1)
Perhaps you could try an alternative and very simple approach?
MyCodebookValueStrip.o_Code1 = Code1.Split(".").Last()
if you're absolutely sure the string does contain a period. Otherwise, use:
MyCodebookValueStrip.o_Code1 = Code1.Split(".").LastOrDefault()
which will return you 'Nothing' if you're string doesn't contain a period.
If your string contains more than one period, you'll get the substring after the last period in the string back. But you do have scope to do otherwise, e.g.:
"StringOne.StringTwo.StringThree".Split(".").First()
will give you "StringOne".
"StringOne.StringTwo.StringThree".Split(".").Last()
will give you "StringThree".
"StringOne.StringTwo.StringThree".Split(".").Skip(1).Take(1)
will give you "StringTwo".
You'll need to reference and import System.Linq to use this stuff, which means you'll need to be using .NET 3.5 or above.
Shouldn't it be:
Code1.Substring(CharNo + 1, (Code1.Length - CharNo - 1))
Because Code1.Length - CharNo gives you an extra character.
Ex:
"abc.abcd"
You want the last 4 characters, and length - charNo will result in 5. Therefore the error.
Dim output As String
Dim Code1 As String = "test.txt"
Dim charNo As Integer = Code1.IndexOf('.')
If ((charNo <> -1) And (Code1.Length <> charNo + 1)) Then
output = Code1.Substring(charNo, Code1.Length - charNo)
Else
output = ""
End If
The above works for me flawlessly.. could it be that you're getting a -1 position from the IndexOf method?
The problem is that you are adding one to the starting index (CharNo + 1), but you don't minus one from the length. To correct it, you should have written:
Code1.Substring(CharNo + 1, (Code1.Length - CharNo - 1))
However, it's unnecessary because all you really needed to do was:
Code1.Substring(CharNo + 1)
Also, you should probably be checking if CharNo + 1 is less than the length, just in case the period was the last character in the text:
If CharNo + 1 < Code1.Length Then
MyCodebookValueStrip.o_Code1 = Code1.Substring(CharNo + 1)
Else
MyCodebookValueStrip.o_Code1 = ""
End If
However, if what you are trying to get is the extension from a file name, you should be using the Path class to do it right (and easier):
MyCodebookValueStrip.o_Code1 = Path.GetExtension(Code1)
Thanks everyone. I should have realised that it needed a -1.
So many right answers here, I'm not sure if I can select more than one as the "accepted answer". I'll give it a try.
Thanks a lot.