Separating a string using Spaces - vb.net

I am having a value "90672.40 91796473.18" in one variable.
I need to store the "90672.40" in an another variable and remaining in another variable.
and also I need to retrieve a numbers from the 40th position to end.
kindly help me..
for your information,
I am having a variable named lsline.
Value of lsline is:
lsline = 15-OCT-08 OTHERS 90672.40 91796473.18
i need to retrieve a number from 40th position, but i dont want the numbers after the space, i just want "90672.40" to store in a another variable.

Use Substring to get part of a string if you don't want to use Split. The following returns 8 characters starting from number 40:
lsline.Substring(40, 8)

Just split into an array
Dim values = Isline.Split(" ")
Dim number = CDec(value[2])

Related

removing double quotes from column header in pandas dataframe

I am trying to understand the intuition behind the following line of code.
I know that it is removing double quotes from the column header within a dataframe, although can anyone please help me understand how it is doing that?
df.columns = [col[1:-1] for col in df.columns]
Thanks
df.columns = ... is the part of line that assigns the list on the right hand side to the columns.
Then the right hand side is a list comprehension, meaning it can be understood like a for loop.
Then, in python, a string is an array of characters. for col in columns mean you iterate over each string in the list of columns. Each col is an array. If that string has quotes, then it looks like "xxxx". So the first and the last elements of the array are quotes.
col[1:-1] is the way to slice the array from the second element to the one before last.
So when you put all these things next to each other, in your case you end up removing quotes.
This is to take 2nd index to 2nd last index in a column names string for each column
Example:-
If column name string is of size less than 3, then it gives you empty string as column name
Example -

Count a Item Integer in a Multi-Line Textbox

I want to play a little with the Textbox. how do I count the items of a textbox? Example: The first line contains the characters: 12 14 16 18 so there are 4 characters but i have one code, and show me 8 character, not 4. how do I display this Count in another textbox? So how do all the characters look into? limited space or comma.
secondTextBox.Text = firstTextBox.Text.Where(Function(x) Not Char.IsWhiteSpace(x)).Count()
this code takes every single digit, I want to take it as a integer. i.e. 12, 14, 16, 18, as a integer.
Try something like this:
Dim number As Integer
secondTextBox.Text = firstTextBox.Text.Split(", ".ToCharArray, StringSplitOptions.RemoveEmptyEntries).Where(Function(x) Integer.TryParse(x, number)).Count
There are lots of ways of doing this, but the simplest which doesn't take account of humans would depend on two things
If all the numbers are on one line and just contain one space between the numbers and no spaces after the last number, just count the number of spaces
To do this in your code you would write
secondTextBox.Text = (firstTextBox.Text.Where(Function(x) Char.IsWhiteSpace(x)).Count() + 1).ToString
If the numbers are on separate lines with no blank lines inbetween the numbers or before or after the numbers, then you would use
secondTextBox.Text = firstTextBox.Lines.Count
When it comes to text searches it may help to take a look at RegEx (Regular Expressions). From your question, it seems you want to count the number of words in a user input. If so, check this question and its first answer.

How to remove all occurences of a substring within a string except the first one visual basic

The code so far looks like this
For i = 0 To (Len(needed) - 1)
If key.Contains(needed(i)) = False Then
key = key + needed(i)
Else
End If
Within the else I would want to remove all occurrences of the substring key(i) from key except the first one. I could do this using a flag variable within a for loop to gain the location of this and simply replace that with "" however I have the entire alphabet and 3 other characters to check for. Is there a better method to do this???
If you wanted to replace any occurrence of a string except the first one, you could get a substring representing the the string after the first one, and run the replace then.
key.Substring(key.IndexOf(key(i)) + 1).Replace(key(i), "")
Hope this helps.

Get the line number of a mutiline textbox that contains a string

For example, if a mutiline textbox has the string "apple" in one of the lines how do I get the line number?
You can use Array.IndexOf:
Dim indexOfText = Array.IndexOf(textBox1.Lines, "apple")
If you want to find a string which can be a part of the line, also searching case-insensitive:
indexOfText = Array.FindIndex(textBox1.Lines, Function(str) str.IndexOf("apple", StringComparison.InvariantCultureIgnoreCase) >= 0)
Since indices are zero based you need to add 1 if you want the line number(in case the index isn't -1).
Another option is to use a RichTextBox which has a GetLineFromCharIndex method which you can use the .Text.IndexOf method to get the index.

Best way to get the first digit from an integer of varying length in VB.NET

I am a newbie to programming and need some help with the basics.
I have a function which takes in an integer value. I want to be able to grab the first digit (or the first and second digits in some cases) of this integer and do something with it.
What is the best way in VB.NET to get the first digit of an integer (or the first and second)?
firstDigit = number.ToString().Substring(0,1)
firstTwoDigits = number.ToString().Substring(0,2);
int.Parse(firstDigit)
int.Parse(firstTwoDigits)
and so forth
I'm not well versed in VB syntax, so forgive me for the syntax errors:
dim i as integer
while i >= 10
i = i \ 10
end while
msgbox "i = " & i
Note, this prints the "first from the left" digit. Like, for "12345" it would print "1".
If you need the digits starting from the end of the integer, just get the modulu result for the tens or the hundreds, according to how many digits you need.
Dim n As Integer
n Mod 10
for the first digit, or:
n Mod 100
for the second and first digits.
If you need the first and second digits from the beginning of the number, there is another answer here which will probably help you.
for first digit you can use:
Dim number As Integer = 234734
Dim first = number.ToString.ToCharArray()(0)
for second digit you can use:
Dim number As Integer = 234734
Dim second = number.ToString.ToCharArray()(1)
This would work. You can use Math.ABS, absolute value, to eliminate negative. The number from left could be replaced by a function if you are using logic, like the overall length of the number, to determine how many of the leading characters you are going to use.
Dim number As Integer = -107
Dim result As String
Dim numberFromLeft As Integer = 2
result = Math.Abs(number).ToString.Substring(0, numberFromLeft)
This results in 10 it is a string but converting it back to a number is easy if you need to. If you need to keep track if it was positive or negative you could use the original value to apply that back to you parsed string.