Insert characters between strings in vb.net - vb.net

I have a string which contains
A F C A D A B A F G H A B C D A E A X B
I want to append a tag before and after A B like [A B]
A F C A D [A B] A F G H [A B] C D A E [A X B] J H A
Working in vb.net windows form

This is definitely not the most elegant solution but I believe it works:
String strInput = "A F C A D A B A F G H A B C D A E A X B J H A A C D A E X B";
StringBuilder sbOutput = new StringBuilder();
StringBuilder sbTemp = new StringBuilder();
foreach (Char ch in strInput)
{
if (ch.Equals('A'))
{
if (sbTemp.Length > 0)
{
sbOutput.Append(sbTemp.ToString());
sbTemp = new StringBuilder().Append(ch);
}
else
{
sbTemp.Append(ch);
}
}
else
{
if (sbTemp.Length > 0)
{
sbTemp.Append(ch);
if (ch.Equals('B'))
{
sbOutput.Append(String.Format("[{0}]", sbTemp.ToString()));
sbTemp = new StringBuilder();
}
}
else
{
sbOutput.Append(ch);
}
}
}

str = str.Replace("A B", "[A B]")

A hard coded replace of "A B" to "[A B]" won't work if you have "A X B", So i would suggest a possible solution, which is regex:
Try these two patterns:
Dim pattern1 as string = #"(A)[^A]*(?=B)"
Dim str as string = Regex.Replace(inputString, pattern1, "[\1"); // make `A` to `[A` followed by a `B`
Dim pattern2 as string = #"(?<=A)[^A]*(B)"
str = Regex.Replace(str, pattern1, "\1]"); // make `B` to `B]` // make `B` to `B]` preceded by `A`
Debug.Print(str);
Hope it helps!

Related

If, Then and Select Case

I'm writing VB Code and I see a question below
There are three positive integers A, B, C
If A is greater than B, C is equal to A+B
If A is less than or equal to B, then C is equal to A-B.
Please use IF...Then and Select/Switch Case to write a program, both of which are used in this program, and additional variables can be added by yourself.
I would like to ask how to write this question, as far as I know, the answer just need only IF Then or Select Case can be realized?
Dim A As Double = 3
Dim B As Double = 2
Dim C As Double = 1
Dim D As Double = 0
D = A - B
Select Case D
Case D > 0
C = A + B
Case D < 0
C = A - B
End Select
If D > 0 Then
C = A + B
ElseIf D < 0 Then
C = A - B
End If
In the real world you wouldn't need to use both an If/Then statement and a Select/Case statement. Since this appears to be homework, I believe the exercise is to see if you can use both conditional check.
I would setup two functions that accept two arguments (a and b) that return the value of c. In your first function use an If/Then and in your second function use a Select/Case.
E.g.
Private Function IfThenOption(a As Integer, b As Integer) As Integer
Dim c As Integer
If (a > b) Then
c = a + b
ElseIf (a < b) Then
c = a - b
Else
c = a
End If
Return c
End Function
Private Function SelectCaseOption(a As Integer, b As Integer) As Integer
Dim c As Integer
Select Case True
Case a > b
c = a + b
Case a < b
c = a - b
Case Else
c = a
End Select
Return c
End Function
Example: https://dotnetfiddle.net/kwcyWc

awk replacement using specific values in two columns

I have a file that looks like this:
20:60479_C_T 60479 C T 0 0 0 0 0 1 0 1
20:60522_T_TC 60522 T TC 0 0 0 0 0 0 0
20:60568_A_C 60568 A C 0 0 1 0 0 1
20:60571_C_A 60571 C A 0 1 0 1 0 0
20:60579_G_A 60579 G A 0 0 1 0 0 0
My current file is bigger with 3 million rows and 3,000 columns. I want to use the values in columns $3 and $4 to replace 0 and 1 in the rest of columns. The desired output would be:
20:60479_C_T 60479 C T C C C C C T C T
20:60522_T_TC 60522 T TC T T T T T T T
20:60568_A_C 60568 A C A A C A A C
20:60571_C_A 60571 C A C A C A C C
20:60579_G_A 60579 G A G G A G G G
I know how to do it for a couple of columns:
awk '{d["0"]=$3; d["1"]=$4; print "20", $1, "0", $2, d[$5], d[$6];}' myfile
But I don't know how to do it automatically for all the columns and avoid adding all the columns manually
$ awk '{d[0]=$3; d[1]=$4; for (i=5; i<=NF; i++) $i=d[$i]} 1' file
20:60479_C_T 60479 C T C C C C C T C T
20:60522_T_TC 60522 T TC T T T T T T T
20:60568_A_C 60568 A C A A C A A C
20:60571_C_A 60571 C A C A C A C C
20:60579_G_A 60579 G A G G A G G G
Since you have a variable number of columns, you can probably get away with something like:
awk <testprog.in '{for (i = 5; i <= NF; i++){$i = $($i+3)}print}'
The "magic" here is the assigning of $($i+3) to $i for all values of i between 5 and the field count (inclusive).
The expression $i+3 will turn 0 and 1 into 3 and 4 respectively and so the next step will be evaluating $3 or $4 (the C and T in the first line for example) and using that to replace the item.
The output of you small test case is, as expected:
20:60479_C_T 60479 C T C C C C C T C T
20:60522_T_TC 60522 T TC T T T T T T T
20:60568_A_C 60568 A C A A C A A C
20:60571_C_A 60571 C A C A C A C C
20:60579_G_A 60579 G A G G A G G G
You will, of course, need to check the performance of this with your larger data sets. On my box, a three-million-line file with 3000 entries each takes about half an hour.
Compare that with a C program (although admittedly quick'n'dirty, heavily tied to your specific input data, without what I would generally consider necessary error checking) which only takes about ten minutes.
For completeness, here's the C variant which, assuming it's called prog.c, you can compile with something like gcc -o prog prog.c and run with something like ./prog <testprog.in:
#include <stdio.h>
#include <ctype.h>
static char buff[102040];
static char *getStr(char *buff, int *pSz) {
if (*buff == 0) return NULL;
char *nextBuff = buff;
while ((nextBuff[0] != 0) && isspace(nextBuff[0])) {
nextBuff++;
}
if (*nextBuff == 0) return NULL;
*pSz = 0;
while ((nextBuff[*pSz] != 0) && ! isspace(nextBuff[*pSz])) {
(*pSz)++;
}
return nextBuff;
}
int main(void) {
char *str, *str3, *str4; int sz, sz3, sz4;
while (fgets(buff, sizeof(buff), stdin) != NULL) {
str = getStr(buff, &sz); printf("%*.*s", sz, sz, str);
str = getStr(str + sz, &sz); printf(" %*.*s", sz, sz, str);
str3 = getStr(str + sz, &sz3); printf(" %*.*s", sz3, sz3, str3);
str4 = getStr(str3 + sz3, &sz4); printf(" %*.*s", sz4, sz4, str4);
str = getStr(str4 + sz4, &sz);
while (str != NULL) {
if (*str == '0') {
printf(" %*.*s", sz3, sz3, str3);
} else {
printf(" %*.*s", sz4, sz4, str4);
}
str = getStr(str + sz, &sz);
}
printf("\n");
}
return 0;
}
Using gsub in awk you could try this as an option:
$ awk '{d[1]=$1;d[2]=$2;gsub(/0/,$3);gsub(/1/,$4);$1=d[1];$2=d[2];}1' myfile
20:60479_C_T 60479 C T C C C C C T C T
20:60522_T_TC 60522 T TC T T T T T T T
20:60568_A_C 60568 A C A A C A A C
20:60571_C_A 60571 C A C A C A C C
20:60579_G_A 60579 G A G G A G G G

Items doesn't display in list box when I run the program

I was doing a program about Bisection Method. I haven't encountered any errors while doing the program, but when I run it, input the data, and press the compute button, no answer is displayed, but a scroll bar appears and freezes the program.
I don't know what is wrong but I guess it has something to do with logical error.
Public Class Form1
Dim a As Double
Dim b As Double
Dim c As Double
Dim fa As Double
Dim fb As Double
Dim fc As Double
Dim err As Double
Dim n As Integer = 1
Dim x As Double
Dim diserr As String
Dim fas As String
Dim fbs As String
Dim fcs As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = TextBox1.Text
b = TextBox2.Text
err = TextBox3.Text
fa = a ^ 3 - a - 3
fb = b ^ 3 - b - 3
c = (a + b) / 2
fc = c ^ 3 - c - 3
x = Math.Abs(a - b)
Do While x > err
fa = a ^ 3 - a - 3
fb = b ^ 3 - b - 3
c = (a + b) / 2
fc = c ^ 3 - c - 3
x = Math.Abs(a - b)
If err < x Then
diserr = "No"
Else
diserr = "Yes"
End If
If fa > 0 Then
fas = "+"
Else
fas = "-"
End If
If fb > 0 Then
fbs = "+"
Else
fbs = "-"
End If
If fc > 0 Then
fcs = "+"
Else
fcs = "-"
End If
If diserr = "Yes" Then
fcs = Str(x)
End If
ListBox1.Items.Add(Str(n))
ListBox2.Items.Add(Str(a))
ListBox3.Items.Add(Str(b))
ListBox4.Items.Add(Str(c))
ListBox5.Items.Add(Str(x))
ListBox6.Items.Add(diserr)
ListBox7.Items.Add(fas)
ListBox8.Items.Add(fbs)
ListBox9.Items.Add(fcs)
n = n + 1
If fas = "-" And fcs = "+" Then
b = c
End If
If fbs = "-" And fcs = "+" Then
a = b
b = c
End If
Loop
End Sub
End Class
You need to consider the cases where fcs is "-".
If I'm not misunderstanding the method you can just replace the last two ifs with
If fas <> fcs Then
b = c
Else
a = b
b = c
End If

Randomly ordering variables

I have 4 strings in variables a,b,c and d. I need to randomly order these variables in such a way so that I can input them into 4 different text boxes but not the same ones every time the program is ran.
I've tried to simplify it for myself by putting the strings into an array. Tell me what I'm doing wrong or if there's a way I could do it much easier.
Private Sub Random()
For i = 1 To 4
If a = 0 Then
a = r.Next(2, 5)
ElseIf b = 0 Then
Do Until b <> a
b = r.Next(2, 5)
Loop
ElseIf c = 0 Then
Do Until c <> a Or c <> b
c = r.Next(2, 5)
Loop
ElseIf d = 0 Then
Do Until d <> a Or d <> b Or d <> c
d = r.Next(2, 5)
Loop
End If
Next
End Sub
Here is one way to do it:
Dim a As String = "a"
Dim b As String = "b"
Dim c As String = "c"
Dim d As String = "d"
Dim all As String() = {a, b, c, d}
Dim random As New Random
Dim allRandom As String() = all.OrderBy(Function() random.Next).ToArray

You have four numbers, how do you figure out which one is greatest?

Is there a very simple algorithm to figure out which of 4 numbers is the greatest?
var lst = new List<int>() { 1, 7, 3, 4 };
var max = lst.Max();
I got no VB, but you get the idea.
If they are in an array, something like this should work:
VB:
Dim ar As Integer() = {3, 6, 9, 12}
Dim largest As Integer = ar(0)
For i As Integer = 1 To ar.Length - 1
If ar(i) > largest Then
largest = ar(i)
End If
Next
C#:
int[] ar = {3, 6, 9, 12};
int largest = ar[0];
for(int i = 1; i < ar.Length; i++) {
if(ar[i] > largest) {
largest = ar[i];
}
}
If you're using a language that supports some sort of max function or array sorting definitely use those features. Or choose any of the other sane answers in this thread. However, just for fun:
maximum = (var1 > var2 ? var1 : var2) > (var3 > var 4 ? var3 : var 4) ?
(var1 > var2 ? var1 : var2) :
(var3 > var 4 ? var3 : var 4);
Put the numbers into an array, sort the array, then select the one whose index is array length -1.
Or you could put the numbers into an array, sort the array, reverse the array, and then select index 0.
If you need to write your own sorting algorithm, the simplest one to implement is likely to be the bubble sort.
With VB.Net you could the following and it will work for any number of numbers
Public Function Max(ParamArray items As Integer()) As Integer
if items.Length = 0 Then
throw New ArgumentException("need at least 1 number")
End IF
return items.Max()
End Function
Then you can now do
Max(1,2,3,4)
There are plenty of ways you could do this.
A really naive approach would be:
#Pseudocode
If number1 > number2 and number1 > number3 and number1 > number4: return number1
Else if number2 > number3 and number2 > number4: return number2
Else if number3 > number4: return number3
Else: return number4
It's more practical to use arrays but if you're starting that could be more complicated than simple if blocks.
If they're in an array - and doing it explicitly rather than using sort:
int max = int.MinValue; // i.e. the "largest" negative number
int largest = -1;
for (int index = 0; index < array.Length; index++)
{
if (array[index] > max)
{
max = array[index];
largest = index;
}
}
The greatest value will be max and it's index in largest.
Nate's answer is more efficient as it uses the first element of the array as the initial value. So the first three lines of my solution would become:
int max = array[0];
int largest = 0;
for (int index = 0; index < array.Length; index++)
Get A, B, C, D from user;
largest = A;
if B > largest then
Largest = B;
if C > largest then
Largest = C;
if D > largest then
largest = D;
Print largest
In Java, if a is an int[4]:
Math.max(Math.max(a[0], a[1]), Math.max(a[2], a[3]))
Dim MyValues As New List(Of Double)
Dim MaxValue As Double
Dim tValue As Double
MyValues.Add(12.58)
MyValues.Add(3.58)
MyValues.Add(518.6)
MyValues.Add(244)
MyValues.Add(31.25)
For Each tValue In MyValues
If MaxValue < tValue Then
MaxValue = tValue
End If
Next
MsgBox(MaxValue)
My first question would be why? Second would be, if it's only four numbers then it really doesn't matter. Whatever takes your fancy. I personally would go with the fewest lines of code. Which would be to use the built in array.Sort method, then take the last item.
I would also consider using LINQ, just because you can.
Or Math.Max in a nasty nested way so Math.Max(Number1,Math.Max(Number2,Math.Max(Number3,Number4))))
If there could be hundreds of numbers, then I would try and pick a better algorithm. Probably the one suggested by #ChrisF, although it would depend on where the numbers are coming from, EG a database could find the max much easier, or if the numbers are being read from somewhere sequentially then you could store the max as you read the numbers.
this is my own analization. i made this code to display the lowest and highest numbers among the 4 inputted numbers from the textbox,. it will display the lowest and highest to appointed labels. if u input two same lowest or highest numbers, a msgbox appear to notify u somehow that u inputted same highest or lowest numbers and it displays back to its appropriate label. i used labels for the display of lowest and highest. here's my fb: iver saladaga anoos , 2ndyear student of JHCSC tambulig, zamboanga del sur, philippines..
so here it is! it worked for me. im using vb6 enterprise edition. :)
Private Sub Command1_Click()
Dim A, B, C, D As Long
A = Text1.Text
B = Text2.Text
C = Text3.Text
D = Text4.Text
If A < B And A < C And A < D Then
Label9.Caption = A
Else
If A > B And A > C And A > D Then
Label10.Caption = A
End If
End If
If A < B And A < D And A < C Then
Label9.Caption = A
Else
If A > B And A > D And A > C Then
Label10.Caption = A
End If
End If
If A < C And A < B And A < D Then
Label9.Caption = A
Else
If A > C And A > B And A > D Then
Label10.Caption = A
End If
End If
If A < C And A < D And A < B Then
Label9.Caption = A
Else
If A > C And A > D And A > B Then
Label10.Caption = A
End If
End If
If A < D And A < C And A < B Then
Label9.Caption = A
Else
If A > D And A > C And A > B Then
Label10.Caption = A
End If
End If
If A < D And A < B And A < C Then
Label9.Caption = A
Else
If A > D And A > B And A > C Then
Label10.Caption = A
End If
End If
If B < C And B < A And B < D Then
Label9.Caption = B
Else
If B > C And B > A And B > D Then
Label10.Caption = B
End If
End If
If B < C And B < D And B < A Then
Label9.Caption = B
Else
If B > C And B > D And B > A Then
Label10.Caption = B
End If
End If
If B < A And B < C And B < D Then
Label9.Caption = B
Else
If B > A And B > C And B > D Then
Label10.Caption = B
End If
End If
If B < A And B < D And B < C Then
Label9.Caption = B
Else
If B > A And B > D And B > C Then
Label10.Caption = B
End If
End If
If B < D And B < C And B < A Then
Label9.Caption = B
Else
If B > D And B > C And B > A Then
Label10.Caption = B
End If
End If
If B < D And B < A And B < C Then
Label9.Caption = B
Else
If B > D And B > A And B > C Then
Label10.Caption = B
End If
End If
If C < A And C < B And C < D Then
Label9.Caption = C
Else
If C > A And C > B And C > D Then
Label10.Caption = C
End If
End If
If C < A And C < D And C < B Then
Label9.Caption = C
Else
If C > A And C > D And C > B Then
Label10.Caption = C
End If
End If
If C < B And C < A And C < D Then
Label9.Caption = C
Else
If C > B And C > A And C > D Then
Label10.Caption = C
End If
End If
If C < B And C < D And C < A Then
Label9.Caption = C
Else
If C > B And C > D And C > A Then
Label10.Caption = C
End If
End If
If C < D And C < A And C < B Then
Label9.Caption = C
Else
If C > D And C > A And C > B Then
Label10.Caption = C
End If
End If
If C < D And C < B And C < A Then
Label9.Caption = C
Else
If C > D And C > B And C > A Then
Label10.Caption = C
End If
End If
If D < A And D < B And D < C Then
Label9.Caption = D
Else
If D > A And D > B And D > C Then
Label10.Caption = D
End If
End If
If D < A And D < C And D < B Then
Label9.Caption = D
Else
If D > A And D > C And D > B Then
Label10.Caption = D
End If
End If
If D < B And D < A And D < C Then
Label9.Caption = D
Else
If D > B And D > A And D > C Then
Label10.Caption = D
End If
End If
If D < B And D < C And D < A Then
Label9.Caption = D
Else
If D > B And D > C And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < B And D < A Then
Label9.Caption = D
Else
If D > C And D > B And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < A And D < B Then
Label9.Caption = D
Else
If D > C And D > A And D > B Then
Label10.Caption = D
End If
End If
Command2.Enabled = True
If A = D And A > C And A > B Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = D And A < C And A < B Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If A = B And A > C And A > D Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = B And A < C And A < D Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If B = D And B > A And B > C Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = D And B < A And B < C Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If B = C And B > D And B > A Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = C And B < D And B < A Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If C = A And C > D And C > B Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = A And C < D And C < B Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
If C = D And C > B And C > A Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = D And C < B And C < A Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
End Sub
Private Sub Command2_Click()
Text1.Text = Clear
Text2.Text = Clear
Text3.Text = Clear
Text4.Text = Clear
Label9.Caption = Clear
Label10.Caption = Clear
Command2.Enabled = False
Command1.Enabled = False
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Command2.Enabled = False
Command1.Enabled = False
End Sub
Private Sub Text1_Change()
Command1.Enabled = True
End Sub