Finding Characters in String - vb.net

So, I'm having this problem and I have no idea how to handle it
Say I have a string with the following format:
"3 6 9 12 13 15 16"
I'm searching for "6" and I find it at position 3, and I remove it.
Next, I search for 6 again, and I find it at position IndexOf(6) (whatever that is). This time I don't want to remove it because it's the 6 in 16.
if string1.contains(6) then
string1 = string1.RemoveAt(string.IndexOf(6),2)
end if
This is vbnet, but any solution to this problem would help.
P.S. This is just a sample code, the main code I'm using has too many things attached to it, and cleaning it for this example would be a nightmare

You asked for a "fancier" solution so I'll give you one:
Dim input As String = "3 6 9 12 13 15 16"
Dim output As String = String.Join(" ", input.Split(" "c).Where(Function(s) s <> "6"))
Debug.WriteLine(output)
3 9 12 13 15 16

There could be more elegant ways of doing this, but if you are processing numbers, convert it to numbers, then you can just look for the number you are interested in.
If you need it in space delimited form, you can always convert it back.
I'm afraid there won't be any "shortcuts" with this one.
Another thing to consider if you are working with actual strings delimited by space, and are looking for patterns, then Regular Expressions is the way to go.
Dim input As String = "3 4 5 6 13 14 15 16"
Dim inputArray() As String = input.Split(" ")
Dim lst As New List(Of Integer)
For Each s In inputArray
lst.Add(Convert.ToInt32(s))
Next
If lst.Contains(6) Then
lst.Remove(6)
End If

A better way to solve the problem is to use regex (tested with sed on Mac OSX):
echo "6 3 6 9 12 13 15 16" | sed -E "s/(6 |[^1-9]6| 6$)//g"
# outputs
3 9 12 13 15 16

Related

How to find every combination of a binary 16 digit number

I have 16 different options in my program and i have a 16 character variable which is filled with 1's or 0's depending on the options that are selected (0000000000000000 means nothing is selected, 0010101010000101 means options 3,5,7,9,14 and 16 are selected, 1111111111111111 means everything is selected.)
When i run my program, the code looks (using an if statement) for a 1 in the designated character of the 16 digit number and if there is one there then it runs the code for that option, otherwise it skips it..
e.g option 3 looks too see if the 3rd character (0010000000000000) is a 1 and if it is it runs the code.
Now what i am trying to do is generate a list of every different combination that is possible so I can create an option for it to just loop through and run every possible option:
0000000000000001
0000000000000010
0000000000000011
...
1111111111111100
1111111111111110
1111111111111111
I have tried this but i think it may take a couple of years to run jaja:
Dim binString As String
Dim binNUM As Decimal = "0.0000000000000001"
Do Until binNUM = 0.11111111111111111
binString = binNUM.ToString
If binString.Contains(1) Then
If binString.Contains(2) Or binString.Contains(3) Or binString.Contains(4) Or binString.Contains(5) Or binString.Contains(6) Or binString.Contains(7) Or binString.Contains(8) Or binString.Contains(9) Then
Else
Debug.Print(binNUM)
End If
End If
binNUM = binNUM + 0.0000000000000001
After the code above is complete i would then take the output list and remove any instances of "0." and then any lines which had fewer than 16 chararcters (because the final character would be a 0 and not show) I would add a 0 until there was 16 characters. I know this bit might be stupid but its as far a ive got
Is there a faster way I can I generate a list like this in VB.net?
You should be able to get the list by using Convert.ToString as follows:
Dim sb As New System.Text.StringBuilder
For i As Integer = 0 To 65535
sb.AppendLine(Convert.ToString(i, 2).PadLeft(16, "0"c))
Next
Debug.Print(sb.ToString())
BTW: This should finish in under one second, depending on your system ;-)
Create an enum with FlagAttributes, which allows you to do the key functions you list. Here is an example of setting it up in a small project I am working on:
<FlagsAttribute>
Public Enum MyFlags As Integer
None = 0
One = 1
Two = 2
Three = 4
Four = 8
Five = 16
Recon = 32
Saboteur = 64
Mine = 128
Headquarters = 256
End Enum
e.g.
Dim temp as MyFlags
Dim doesIt as Boolean
temp = MyFlags.One
doesIt = temp.HasFlag(MyFlags.Two)
temp = temp OR MyFlags.Three
'etc.
The real advantage is how it prints out, if you want something other than 0, 1 and is much more human friendly.

How to convert string to byte in Visual Basic

I'm trying to simulate an algoritham in cryptography and I need to convert a string of 0s and 1s back into a word. Example:
I have: 01011110010101101000001101100001101
I have split it into an array of strings:
0101111, 0010101, ...
each member has 7 characters. I want to get a letter that 0101111 represents in UTF8? How do I do this?
I try CType("0010101", Byte), but it fails. I can pass max 111 this way.
Help :/
UTF-8 is 8 bit, those are only 7 bits. Do you mean 7 bit ASCII?
In that case here you go:
Function BinToStr(binStr As String) As String
Dim i As Long
For i = 0 To (Len(binStr) / 7) - 1
[A1] = CLng(Mid(binStr, i * 7 + 1, 7))
BinToStr = BinToStr & Chr([BIN2DEC(A1)])
Next
End Function
If that's not what you're looking for, let me know.

Get element in binary search tree given a path of ones and zeros

I have a string of ones and zeros, zero mean left and ones mean right, and I need to return the item to the end of the path. If the path leads to nothing returns null.
Tree:
15
/ \
12 18
/ / \
10 16 20
\ \
11 17
String 001 will return 11
String 101 will return 17
String 1111 will return null
How can I write this method in Java?
you can traverse among this tree and check for each character of the string if it is 0 then getleft() and if it is 1 then getright()
and so on, i hope this help :D
i have a midterm tomorrow so i don't have time to write it's implementation now :D

creating a variable that change sizes in for loop

I have to create a fits file using the data from two IDL structures. This is not the basic problem.
My problem is that first I have to create a variable that contains the two structures.
To create this I used a for loop that will write at each step a new row of my variable.
The problem is that I cannot add the new row at the next step, it overwrite it so at the end my fits file instead of having, I don't know, 10000 rows, it has only one row.
This is what I also tried
for jj=0,h[1]-1 do begin
test[*,jj] = [sme.wave[jj], sme.smod[jj]]
print,test
endfor
but the * wildcard is messing up everything because now inside test I have the number corresponding to jj, not the values of sme.wave and sme.smod.
I hope that someone can understand what I asked and that can help me!
thank you in advance!
Chiara
Assuming your "sme.wave" and "sme.smod" structure fields contain 1-D arrays with the same number of elements as there are rows in "test", then your code should work. For example, I tried this and got the following output:
IDL> test = intarr(2, 10) ; all zeros
IDL> sme = {wave:indgen(10), smod:indgen(10)*2}
IDL> for jj=0, 9 do test[*,jj] = [sme.wave[jj], sme.smod[jj]]
IDL> print, test
0 0
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
However, for better speed optimization, you should instead do the following and take advantage of IDL's multi-threaded array operations. Looping is typically much slower than something like the following:
IDL> test = intarr(2, 10) ; all zeros
IDL> sme = {wave:indgen(10), smod:indgen(10)*2}
IDL> test[0,*] = sme.wave
IDL> test[1,*] = sme.smod
IDL> print, test
0 0
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
Further, if you don't know what the size of "test" is ahead of time, and you want to append to the variable, i.e. add a row, then you can do this:
IDL> test = []
IDL> sme = {wave:Indgen(10), smod:Indgen(10)*2}
IDL> for jj=0, 9 do test = [[test], [sme.wave[jj], sme.smod[jj]]]
IDL> Print, test
0 0
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18

Problem with File IO and splitting strings with Environment.NewLine in VB.Net

I was experimenting with basic VB.Net File IO and String splitting. I encountered this problem. I don't know whether it has something to do with the File IO or String splitting.
I am writing text to a file like so
Dim sWriter As New StreamWriter("Data.txt")
sWriter.WriteLine("FirstItem")
sWriter.WriteLine("SecondItem")
sWriter.WriteLine("ThirdItem")
sWriter.Close()
Then, I am reading the text from the file
Dim sReader As New StreamReader("Data.txt")
Dim fileContents As String = sReader.ReadToEnd()
sReader.Close()
Now, I am splitting fileContents using Environment.NewLine as the delimiter.
Dim tempStr() As String = fileContents.Split(Environment.NewLine)
When I print the resulting Array, I get some weird results
For Each str As String In tempStr
Console.WriteLine("*" + str + "*")
Next
I added the *s to the beginning and end of the Array items during printing, to find out what is going on. Since NewLine is used as the delimiter, I expected the strings in the Array to NOT have any NewLine's. But the output was this -
*FirstItem*
*
SecondItem*
*
ThirdItem*
*
*
Shouldn't it be this -
*FirstItem*
*SecondItem*
*ThirdItem*
**
??
Why is there a new line in the beginning of all but the first string?
Update: I did a character by character print of fileContents and got this -
F - 70
i - 105
r - 114
s - 115
t - 116
I - 73
t - 116
e - 101
m - 109
- 13
- 10
S - 83
e - 101
c - 99
o - 111
n - 110
d - 100
I - 73
t - 116
e - 101
m - 109
- 13
- 10
T - 84
h - 104
i - 105
r - 114
d - 100
I - 73
t - 116
e - 101
m - 109
- 13
- 10
It seems 'Environment.NewLine' consists of
- 13
- 10
13 and 10.. I understand. But the empty space in between? I don't know whether it is coming due to printing to the console or is really a part of NewLine.
So, when splitting, only the character equivalent of ASCII value 13, which is the first character of NewLine, is used as delimiter (as explained in the replies) and the remaining stuff is still present in the strings. For some reason, the mysterious empty space in the list above and ASCII value 10 together result in a new line being printed.
Now it is clear. Thanks for the help. :)
First of all, yes, WriteLine tacks on a newline to the end of the string, hence the blank line at the end.
The problem is the way you're calling fileContents.Split(). The only version of that function that takes only one argument takes a char(), not a string. Environment.NewLine is a string, not a char, so (assuming you have Option Strict Off) when you're calling the function it's implicitly converting it to a char, using only the first character in the string. This means that instead of splitting your string on the actual sequence of two characters that make up Environment.NewLine, it's actually splitting only on the first of those characters.
To get your desired output, you need to call it like this:
Dim delims() as String = { Environment.NewLine }
Dim tempStr() As String = fileContents.Split(delims, _
StringSplitOptions.RemoveEmptyEntries)
This will cause it to split on the actual string, rather than the first character as it's doing now, and it will remove any blank entries from the results.
Why not just use File.ReadAllLines? One single call reads the file and returns a string array with the lines.
Dim tempStr() As String = File.ReadAllLines("data.txt")
I just ran into the same issue, and found all the comments very helpful. However, I corrected my issue by replacing "Environment.NewLine" with vbLF (as opposed to vbCrLf, which had the same issue). Any issues with this approach? (It seems more straight forward, but I'm not a programmer, so I wouldn't know of any potential issues).