Getting value in the middle of a string - vb.net

I am try to get value in the middle of a string
My string is
"arely browning,68097,19
I know I can use a substring and pick a specific place but I want it to work for any line that is format like this

If the format of the string remains the same, then this will do it:
Dim MyString As String = "arely browning,68097,19"
Dim StringPart As String = Mid(MyString, InStr(MyString, " "), InStr(MyString, ",") - InStr(MyString, " "))

Related

select only text between quotes in VB

Is it some function which can return me text between qoutes.? Text before and between quotes is variable length. I find a function mid but in specify is length. I would like to get from this string text between both quots(APP_STATUS_RUNNING) and (PRIMARY):
string: Error gim_icon_cfg_1 Application with DBID 736 has status "APP_STATUS_RUNNING", runmode "PRIMARY"
Thank you
EDIT: I try to get output to the label but show me error:BC30452: Operator '&' is not defined for types 'String' and '1-dimensional array of String'.
Dim output As String = myProcess.StandardOutput.ReadToEnd()
Dim StandardError As String = myProcess.StandardError.ReadToEnd()
Dim Splitted() As String
Splitted = Split(output, """")
Label1.text="Ahoj " & Splitted & "Error " & StandardError
You can split your string by quotes beeing the delimiter Split(InputString, """") the odd numbers of the output array then are the strings between quotes, the even numbers are the rest.
Option Explicit
Public Sub Example()
Dim InputString As String
InputString = "Error gim_icon_cfg_1 Application with DBID 736 has status ""APP_STATUS_RUNNING"", runmode ""PRIMARY"""
Dim Splitted() As String
Splitted = Split(InputString, """")
' between quotes (all odd numbers)
Debug.Print Splitted(1) ' APP_STATUS_RUNNING
Debug.Print Splitted(3) ' PRIMARY
' rest (all even numbers)
Debug.Print Splitted(0) ' Error gim_icon_cfg_1 Application with DBID 736 has status
Debug.Print Splitted(2) ' , runmode
End Sub
Or:
The follow code gives you what you are trying to have.
In practice you can search patterns that are between quotes then get from each element the first element sliced by quote (which means quote = 0 element = 1 other quote = 2)
Dim s As String = "Error gim_icon_cfg_1 Application With DBID 736 has status ""APP_STATUS_RUNNING"", runmode ""PRIMARY"""
Dim parts() As String = s.Split(" "c).Where(Function(el) el Like "*""*""*").Select(Function(el) el.Split(""""c)(1)).ToArray

reverse some text in cell selected - VBA

I'm rooky for VBA. I have some problem about reversing my data on VBA-Excel. My data is "3>8 , 6>15 , 26>41 (each data on difference cells)" that i could reverse "3>8" to "8>3" follow my requirement by using function reverse. But i couldn't reverse "6>15" and "26>41" to "15>6" and "41>26". It will be "51>6" and "14>62" that failure, I want to be "15>6" and "41>26".
Reverse = StrReverse(Trim(str))
Help me for solve my issue please and thank for comment.
You first need to find the position of the ">" in the cell. you do this by taking the contents of the cell and treating it as a String and finding the ">"
This is done in the line beginning arrowPosition. This is the integer value of the position of the ">" in you original string
Next use Left to extract the text up to the ">" and Right to extract the text after the ">"
Then build a new String of rightstr & ">" & leftStr.
Note I input my data from Sheet1 B5 but you can just use any source as long as it is a String in the correct format.
Sub Test()
Dim myString As String
myString = Sheets("Sheet1").Range("B5")
Debug.Print myString
Debug.Print reverseString(myString)
End Sub
Function reverseString(inputString As String) As String
Dim leftStr As String
Dim rightStr As String
Dim arrowPosition As Integer
arrowPosition = InStr(1, inputString, ">")
leftStr = Left(inputString, arrowPosition - 1)
rightStr = Right(inputString, Len(inputString) - arrowPosition)
reverseString = rightStr & ">" & leftStr
End Function
just because you look for a VBA, you can add this function into your code:
Function rev(t As String) As String
s = Split(t, ">", 2)
rev = s(1) & ">" & s(0)
End Function
of course only if you have to reverse 2 number, otherwise you'll loop the "s", but the function would lose its usefulness

Parsing numbers containing commas or periods

I have three values which need to be sorted from highest to lowest value. I use the following code which works like a charm until I want to use periods "." and commas ",". If I type "1,3" it displays as I like, but if I type "1.3" it changes to 13. My end users need to be able to use both commas and periods.
How can I fix this?
Dim IntArr(2) As Decimal
IntArr(0) = TextBox1.Text
IntArr(1) = TextBox2.Text
IntArr(2) = TextBox3.Text
Array.Sort(IntArr)
Dim highestNum As Decimal
Dim Midelnum As Decimal
Dim lowestNum As Decimal
lowestNum = IntArr(0)
Midelnum = IntArr(1)
highestNum = IntArr(2)
MsgBox("Highest " & highestNum)
MsgBox("lowest " & lowestNum)
MsgBox("middel " & Midelnum)
The problem is that it's based on culture. I say this because if I enter numbers as you described, I get the opposite effect ("1,3" -> "13", etc).
Here's a quick way to change the values to match the current culture.
At the top of your class, put this:
Imports System.Globalization
Then, you can do this:
Dim IntArr(2) As Decimal
Dim nfi As NumberFormatInfo = CultureInfo.CurrentCulture.NumberFormat
Dim sep1 As String = nfi.NumberDecimalSeparator
Dim sep2 As String = If(sep1.Equals("."), ",", ".")
Dim useComma As Boolean = (TextBox1.Text.Contains(",") Or TextBox2.Text.Contains(",") Or TextBox3.Text.Contains(","))
'Replace the separator to match the current culture for parsing
Decimal.TryParse(TextBox1.Text.Replace(sep2, sep1), IntArr(0))
Decimal.TryParse(TextBox2.Text.Replace(sep2, sep1), IntArr(1))
Decimal.TryParse(TextBox3.Text.Replace(sep2, sep1), IntArr(2))
Array.Sort(IntArr)
sep1 = If(useComma, ",", ".")
sep2 = If(useComma, ".", ",")
'Reformat the results to match the user's input
Dim lowestNum As String = IntArr(0).ToString().Replace(sep2, sep1)
Dim middleNum As String = IntArr(1).ToString().Replace(sep2, sep1)
Dim highestNum As String = IntArr(2).ToString().Replace(sep2, sep1)
Dim msg As String = "Highest: {0}" & Environment.NewLine & _
"Lowest: {1}" & Environment.NewLine & _
"Middle: {2}"
msg = String.Format(msg, highestNum, lowestNum, middleNum)
MessageBox.Show(msg)
Also, since you are using .NET, you may want to skip the VB6 way of doing things. Refer to my example to see what I've used.
You could use the hack of altering the string before saving it:
TextBox.Text.Replace(".",",")
But if you want to show the original input you could have a variable to detect the entered character:
Dim isDot As Boolean = False
Dim number As String = TextBox.Text
If number.Contains(".") Then
isDot = True
End If
And in the end replace it just for purposes of displaying
If isDot Then
number.Replace(",",".")
End If
The accepted answer uses too much unnecessary string manipulation. You can use the CultureInfo object to get what you need:
Sub Main
Dim DecArr(2) As Decimal
'Select the input culture (German in this case)
Dim inputCulture As CultureInfo = CultureInfo.GetCultureInfo("de-DE")
Dim text1 As String = "1,2"
Dim text2 As String = "5,8"
Dim text3 As String = "4,567"
'Use the input culture to parse the strings.
'Side Note: It is best practice to check the return value from TryParse
' to make sure the parsing actually succeeded.
Decimal.TryParse(text1, NumberStyles.Number, inputCulture, DecArr(0))
Decimal.TryParse(text2, NumberStyles.Number, inputCulture, DecArr(1))
Decimal.TryParse(text3, NumberStyles.Number, inputCulture, DecArr(2))
Array.Sort(DecArr)
Dim format As String = "Highest: {0}" & Environment.NewLine & _
"Lowest: {1}" & Environment.NewLine & _
"Middle: {2}"
'Select the output culture (US english in this case)
Dim ouputCulture As CultureInfo = CultureInfo.GetCultureInfo("en-US")
Dim msg As String = String.Format(ouputCulture, format, DecArr(2), DecArr(1), DecArr(0))
Console.WriteLine(msg)
End Sub
This code outputs:
Highest: 5.8
Lowest: 4.567
Middle: 1.2

Input string was not in a correct format while adding string to integer

I have a code like this :
Dim mincatval As String
Dim strarr() As String = dr1(0).ToString().Split(New Char() {"-"c})
Dim i As String
i = (Integer.Parse(strarr(0)) + 1)
mincatval = i
my dr(1) value is L1 i want to add 1,so i want the out put L2,but i am getting error like this :Input string was not in a correct format.
Supposing that strarr(0) is the word "L1" and you want it to become "L2" then you need to isolate the numeric part from the text part and then rebuild the string taking the first part of strarr and the incremented value
Dim mincatval As String
Dim strarr() As String = dr1(0).ToString().Split(New Char() {"-"c})
Dim i As String
Dim itShouldBeAnumber = strarr(0).Substring(1)
if Int32.TryParse(itShouldBeAnumber, i) Then
mincatval = strarr(0).Substring(0,1) & (i + 1)
else
MessageBox.Show("Not a valid number from position 1 of " & strarr(0))
End if
Of course this solution assumes that your string is Always composed of an initial letter followed by a numeric value that could be interpreted as an integer

Split( "TEXT" , "+" OR "-" ,2)

I need to separate a string with Visual Basic.
The downside is that i have more then one separator.
One is "+" and the other one is "-".
I need the code to check for the string if "+" is the one in the string then use "+"
if "-" is in the string then use "-" as separator.
Can I do this?
For example: Split( "TEXT" , "+" OR "-" ,2)
easiest way is to replace out the second character and then split by only one:
Dim txt As String, updTxt As String
Dim splitTxt() As String
updTxt = Replace(txt, "-", "+")
splitTxt = Split(updTxt, "+")
or more complex. The below returns a collection of the parts after being split. Allows you to cusomize the return data a bit more if you require:
Dim txt As String, outerTxt As Variant, innerTxt As Variant
Dim splitOuterTxt() As String
Dim allData As New Collection
txt = "test + test - testing + ewfwefwef - fwefwefwf"
splitOuterTxt = Split(txt, "+")
For Each outerTxt In splitOuterTxt
For Each innerTxt In Split(outerTxt, "-")
allData.Add innerTxt
Next innerTxt
Next outerTxt
What you describe seems pretty straightforward. Just check if the text contains a + to decide which separator you should use.
Try this code:
dim separator as String
separator = Iif(InStr(txt, "+") <> 0, "+", "-")
splitResult = Split(txt, separator, 2)
The code assumes that the text you want to split is in the txt variable.
Please note that I don't have VBA here and wasn't able to actually run the code. If you get any error, let me know.