I need to split and edit text from string to a certain formula.
The text String is:
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
And I need it to be:
1;2;3;4;5
<br>
1;2;3;4;5
<br>
1;2;3;4;5
<br>
1;2;3;4;5
<br>
1;2;3;4;5
<br>
1;2;3;4;5
<br>
In 1 string i have like 50 of those I need it to be lines of that formula.
Ive tried to use split by new lines and adding chars to every word and then split by char but it doesnt helped me.
Assuming that the line breaks are given as a CR/LF sequence
Dim result = input.Replace(vbCrLf, ";")
or
Dim result = input.Replace(Environment.NewLine, ";")
See: https://dotnetfiddle.net/pB08ay
Your updated more complex input string can be processed like this
Dim result = input.Replace(Environment.NewLine, ";").
Replace(";;", Environment.NewLine & "<br>" & Environment.NewLine)
We begin as before. This produces a string looking like 1;2;3;4;5;;1;2;3;4;5;;1;2;3;4;5;;1;2;3;4;5;;1;2;3;4;5.
Then we replace the double semicolon by the <br> embedded between two line breaks.
Try this function below:
Private Function FormatString(ByVal InputString As String) As String
InputString = InputString.Replace(vbCrLf, "")
For i = 0 To InputString.Length - 1
InputString = InputString.Insert(i + i, ";")
Next
InputString = InputString.Remove(0, 1)
Return InputString
End Function
Related
I have a problem with replacing a number in a string.
The code is:
Dim Searchstring As String= "2 7 12 33 4 15 22 30 15"
Dim Pattern As String= "^([0-9]$)"
Dim Match As String = Val(TextBox1.Text)
Dim ReplacementString As String = "0"
Dim rgx As Regex = New Regex(pattern)
Dim NewString As String
NewString = Regex.Replace(Searchstring, Match, ReplacementString)
RichTextBox2.Text = NewString
The problem is that when I input a number, say "2", in the textbox and run the program, the replace will be as follows in the richtextbox2.text: 0 7 10 33 4 15 00 30 15 instead of 0 7 12 33 4 15 22 30 15
But when I input the number of "22" or "12" then only these numbers will be replaced and that is okay.
So how can I find a pattern that replaces only the number "2" if I want that and only that, and not change the "12" or "22"?
Please can you help me with this case?
The appropriate pattern would be ^([0-9]+) as it would get the first digits and replace them with a 0. Actually, this '$' character represents the end of a string. If you only need to replace the first number, it shouldn't be there.
The '+' character means "One ore more from the previous pattern", then it will search for one or more digits in the "[0-9]" scope from the beginning of the text and capture only the digits.
You need to tell it how to delimit the item you want to replace, in this case the "word boundary"* entity \b will work:
Dim searchstring As String = "2 7 12 33 4 15 22 30 15"
Dim match As String = TextBox1.Text
Dim pattern = "\b" & Regex.Escape(match) & "\b"
Dim rgx As Regex = New Regex(pattern)
Dim replacementString As String = "0"
Dim newString = rgx.Replace(searchstring, replacementString)
RichTextBox2.Text = newString
I used Regex.Escape on the search term in case you want to replace something like a . or ?, which have special meanings in regular expressions; escaping them removes the special meaning and uses them as ordinary characters.
You can see that it replaces only the "2" when you enter "2" in the TextBox:
and it replaces both occurrences of "15" when you tell it to:
* What is a word boundary in regex?
To find any number in the string, you can use \b to denote the beginning or end of a word (i.e., a sequence of letters, digits and underscores).
Example: replace only the number 12:
\b12\b
This will not replace, e.g., in 123 or 412.
While I will not disagree with the answers that have been presented, I will assert that the easiest and most obvious choice would be to use built-in .NET methods to manipulate your string.
Your input is a string that contains numbers delimited by spaces. You then want to replace an exact numeric match with some replacement.
You can do this by splitting the string, looping over the array, modifying the currently iterated items that match your value, and then joining them back:
Dim searchString As String = "2 7 12 33 4 15 22 30 15"
Dim replacementString As String = "0"
If (Not Integer.TryParse(TextBox1.Text, Nothing)) Then
MessageBox.Show("Please enter a valid integer.")
Return
End If
Dim searchStringCollection() As String = searchString.Split(" "c)
For index As Integer = 0 To searchStringCollection.Length - 1
If (searchStringCollection(index) = TextBox1.Text) Then
searchStringCollection(index) = replacementString
End If
Next
Dim newString As String = String.Join(" ", searchStringCollection)
Fiddle: https://dotnetfiddle.net/dZfsy6
Newbie question. I can't figure this out.
I have string "48,1;49,2;50,0" which I have split using :
Dim splitAtSemiColon() As String = strPatientMed.Split(";"c)
Dim result() = splitAtSemiColon.Select(Function(x) x.Split(","c)).ToArray()
In the Visual studio interface, I can see that this results in:
Result(0) contains (0) = "48" and (1) = "1"
Result(1) contains (0) = "49" and (1) = "2"
Result(2) contains (0) = "50" and (1) = "0"
But how do I loop to read this array using code i.e. how do I read the (0) and (1) portion of each result item?
Thanks all!
Sub Main()
Dim strPatientmed = "48,1;49,2;50,0"
Dim splitAtSemiColon() As String = strPatientMed.Split(";"c)
Dim result() = splitAtSemiColon.Select(Function(x) x.Split(","c)).ToArray()
'Result is an Array of arrays, each inner array has only 2 elements (first & last)
For Each element In result
'For each <array> in result
'result(0) -> <element> -> [48|1]
'<element>.First = 48 | <element>.Last = 1
Console.WriteLine($"Before comma: {element.First} | After comma: {element.Last}")
Next
End Sub
Result
Before comma: 48 | After comma: 1
Before comma: 49 | After comma: 2
Before comma: 50 | After comma: 0
I want to do the following thing: if I have a Textbox1.Lines:
2
5
15
21
45
and I want to for example get the number left 15 and get the number right 15.
Output: Textbox2.Lines 15 21
5 15
How should I do this? example for the number 2 want substring 2 5
Module Module1
Public Function FirstWords(input As String,
count As Integer) As String
Dim words = count
For i As Integer = 0 To input.Length - 1
' Decrement word count when we reach a space.
If input(i) = " " Then
words -= 1
End If
' When no words remaining, return a substring to this point.
If words = 0 Then
Return input.Substring(0, i)
End If
Next
Return ""
End Function
how should i do this to work?
You can separate them into an array of strings with the Split function, like in this example:
Dim input as String = "2 5 15 21 45"
Dim numbers as String() = input.Split(" ")
For Each s as String In numbers
Console.WriteLine(s)
Next
You just have to figure out how to extract the numbers you want from the lot. Have fun!
Textbox1.Lines =
2
4
11
13
19
21
Textbox2.Lines =
3
5
14
17
26
29
In output (Textbox3.text) I want the following issue:
2 3
4 5
11 14
13 17
19 26
21 29
so how do I get this to work?
Dim linesx() As String = TxtStringNumP1.Lines
Dim linesy() As String = TxtStringNumP2.Lines
For x As Integer = 1 To linesx.Length - 1
For y As Integer = 1 To linesx.Length - 1
Dim strWords1 = linesx(y).Split(",")
Dim strWords2 = linesy(y).Split(",")
TextBoxO1.Text &= Val(strWords1(0)) + vbCrLf + Val(strWords2(0) & vbNewLine)
Next
Next
Remove the vbCrLf as you don't want a new line between the first value & the second. Then, you also don't want a second loop, otherwise you are looping through the values multiple times. You also don't need to split the values by commas if they aren't going to contain commas
Finally, the value needs to be appended to the text already in TextBox01 otherwise its value is just being replaced. The TextBox01.Clear() ensures that it is empty to begin with.
TextBox01.Clear()
Dim linesx() As String = TxtStringNumP1.Lines
Dim linesy() As String = TxtStringNumP2.Lines
For x As Integer = 0 To linesx.Length - 1
Dim strWords1 As String = linesx(x)
Dim strWords2 As String = linesy(x)
TextBoxO1.Text = TextBoxO1.Text & strWords1 & " " & strWords2 & vbCrLf
Next
The above code assumes both text boxes will have the same amount of lines. If this isn't the case, you will need to check that a line exists before trying to assign it to one of your strWords variables. You will also need to loop to the larger of the 2 Lines.Length
I have just started to learn Visual Basic and I am having trouble with a loop. What I want it to do is print out the string "ABCDEFG" into a list box then remove the last character and print it out until only "A" is left.
This is the code I am using:
Dim abc As String = "ABCDEFG"
For i = 0 To 5
abc.Substring(0, abc.Length - 1)
lstabc.Items.Add(abc)
Next i
The desired result would look like this but all i get is lines of "ABCDEFG"
ABCDEFG
ABCDEF
ABCDE
ABCD
ABC
AB
A
You're never assigning anything different to abc, so it's always adding the full string. Also, you are not specifying a different length to substring. Try this.
Dim abc As String = "ABCDEFG"
Dim abc_alt as String
For i = 0 To abc.Length - 1
abc_alt = abc.Substring(0, abc.Length - i)
lstabc.Items.Add(abc_alt)
Next i
String in c#, vb.net are non mutable. So you need to store the result in another variable and print that variable.
Dim substr As String
substr = abc.Substring(0, abc.Length - i)
lstabc.Items.Add(substr)