VB.Net Paragraph Space - vb.net

So my code is like this :
richtextbox1.text = x1.text + x2.text (x's are labels)
And the result is like this :
x1x2
Expected Output :
x1
x2

You have to use NewLine Char to put extra line between two string.
richTextBox1.Text = string1 + System.Environment.NewLine + string2

Hi guys I am not good at this but this worked for me:
1. declare string 1 and string 2 just before the if statement like this using Dim:
Dim string1 = "I am teacher."
Dim string2 = "I like my students."
If TextBox1.Text = "write here the word that you want to search" Then
RichTextBox1.Text = "I am teacher." + System.Environment.NewLine + "I like my students."
End If
I am teacher. is an example of a paragraph1
I like my students. is an example of a paragraph2
Now here is the if statement with the two paragraphs above:

Try this
RichTextbox1.Text = x1.Text & " " &

Related

How to get the part inside an "[" and "]" of a string (strange behaviour)

I am trying to get the part of a string between [ and ]. So far I have this:
For Each a As String In CType(TabControl1.SelectedTab.Controls.Item(0),
RichTextBox).Text.Split(System.Environment.NewLine())
If (a.Contains("print ")) Then
Dim sData As String
sData = a.Substring(6)
Dim i, j As Integer
i = sData.IndexOf("[") + "[".Length
j = sData.IndexOf("]") - i
Dim sData1 As String
sData1 = sData.Substring(i, j)
Console.WriteLine(sData1)
End If
Next
However, if I have this:
print [Hello world!]
print [What's up world?]
then the output is this:
Hello world!
but the required output is:
Hello world!
What's up world
I.e. it will not display anything after the first time a print is found.
So why is that happening and how could I fix it?
You are not using Option Strict On which results in you not being told that Split(System.Environment.NewLine()) is truncating the CRLF to CR. The lines of a RichTextBox are separated with LF.
The easy solution would be to use the .Lines property of the RTB:
For Each a As String In CType(TabControl1.SelectedTab.Controls.Item(0), RichTextBox).Lines
U can go with regex but here's a non-regex solution
'U can also use a loop(This is a working solution)
Dim mystring = "print [Hello world]" + Environment.NewLine + "Print [What's up world?]" + ........
For Each line In mystring.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
Dim hloWorld = line.Split("[")(1)
Dim result = hloWorld.Replace("]", "")
Dim finalResult = result + Environment.NewLine
'''Do what u wanna do with ur final result
Next

Add text before specific characters

str as String = " " +"thisrocks" + " "
and strArray(0) = 123456sdv :'++':
so i want to add str before the :'++':, and then
strArray(0) = 123456sdv thisrocks :'++':
Is it possible ?
What could I do to search for it ? Regex maybe ?
str and strArray will already be there from previous codes. I just want to combine it int he right place.
Using the space in between will not be helpful as the strArray(0) could also be, dsf dsv dsgvsvs svs svssd bdsb sbdfb bsbb sb s sbsfbfsbsbfs :'++': and so on.
I can't control it as they come like that from previous codes and there is no way to fix them :/
I can't generalize the question since it is not clear enough, for this occasion you can use the following code to insert a string in between these two words
Dim str As String = " " + "thisrocks" + " "
Dim strArray(10) As String
strArray(0) = "123456sdv :'++':"
strArray(0) = strArray(0).Replace(":'++':", str & ":'++':")
Output will be
"123456sdv thisrocks :'++':"
Note:
this will work as, replace :'++': with & str and add :'++': to it so :'++': will stay their for the next replacement.
You can use String.IndexOf to find where the marker :'++': is and String.Insert to insert the required data:
Dim sample As String = "123456sdv :'++':"
Dim insertData As String = " thisrocks "
Dim marker As String = ":'++':"
Dim insertPos As Integer = sample.IndexOf(marker)
If insertPos >= 0 Then
sample = sample.Insert(insertPos, insertData)
End If
Console.WriteLine(sample) ' outputs "123456sdv thisrocks :'++':"

My For Loop skips my IF statement

I wonder if anyone can help. I am making a program that will convert Text to ASCII. However, I want my program to ignore spaces. Hence, "IT WAS A" should look like this: 7384 876583 65
When I use the Step Into Feature of VB I can see that my For loop is skipping my IF statement which should be giving me my spaces. I don't understand why. As you can probably tell, I am a beginner so any specific help would be greatly appreciated. My code looks like this:
Dim PlainText, ConvertedLetter As String
Dim LetterToConvert As Char
Dim AscNumber, Counter As Integer
ConvertedLetter = ""
PlainText = txtPlain.Text
For Counter = 1 To Len(PlainText)
LetterToConvert = Mid(PlainText, Counter, 1)
If PlainText = " " Then
ConvertedLetter = " "
Else : AscNumber = Asc(LetterToConvert)
ConvertedLetter = ConvertedLetter & AscNumber
End If
Next
txtAscii.Text = ConvertedLetter
Because you're comparing PlainText, which is the whole string, to " ". It would need to be:
If LetterToConvert = " " Then ....
Try this:
Dim PlainText, ConvertedLetter As String
ConvertedLetter = ""
PlainText = "IT WAS A"
For Each c As Char In PlainText 'iterate through each character in the input
If c <> " " Then ' check whether c is space or not
ConvertedLetter &= Asc(c).ToString()' ascii value is taken if c<>" "
Else
ConvertedLetter &= " " ' c is space means add a space
End If
Next
MsgBox(ConvertedLetter) ' display the result
You will get the output as
7384 876583 65

VB.NET: Convert string containing "Lastname, Firstname" to "Firstname Lastname"

I'm trying to convert a string that contains someones name as "Last, First" to "First Last".
This is how I am doing it now:
name = name.Trim
name = name.Substring(name.IndexOf(",") + 1, name.Length) & " " & name.Substring(0, name.IndexOf(",") - 1)
When I do this I get the following error:
ArgumentOutOfRangeException was unhandled
Index and length must refer to a location within the string
Parameter name: length
Can someone explain why I am getting this error and how I should be doing this?
You are getting error on this:
name.Substring(name.IndexOf(",") + 1, name.Length)
name.Length should have subtracted with the length of the string before the comma.
The best way for that is to split the string.
Dim oFullname as string = "Last, First"
Dim oStr() as string = oFullname.split(","c)
oFullname = oStr(1).trim & " " & oStr(0).trim
MsgBox (oFullname)
The second parameter for String.Substring is the length of the substring, not the end position. For this reason, you're always going to go out of bounds if you do str.Substring(n, str.Length) with n > 0 (which would be the whole point of a substring).
You need to subtract name.IndexOf(",") + 1 from name.Length in your first substring. Or just split the string, as the others have suggested.
simply ,you only need to split the string
Dim originalName As String = "Last,First"
Dim parts = name.Split(","C)
Dim name As String = parts(1) & " " & parts(0)
If you're using the Unix command line--like the terminal on a Mac--you can do it like this:
Let's say that you have a file containing your last-comma-space-first type names like this:
Last1, First1
Last2, First2
Last3, First3
OK, now let's save it as last_comma_space_first.txt. At this point you can use this command I came up with for your particular problem:
sed -E 's/([A-Za-z0-9]+), ([A-Za-z0-9]+)/\2 \1/g' last_comma_space_first.txt > first_space_last.txt
--->>> Scroll --->>>
You're done! Now, go check that first_space_last.txt file! ^_^ You should get the following:
First1 Last1
First2 Last2
First3 Last3
Tell your friends... Or don't...
This would work keeping to the posters format.
Name = "Doe,John"
Name = Replace(Name.Substring(Name.IndexOf(","), Name.Length - Name.IndexOf(",")) & " " & Name.Substring(0, Name.IndexOf(",")), ",", "")
Result Name = "John Doe"

Trim last "," delimiter of a string in VB.NET

This is my code:
With ad.Tables(2)
For i As Integer = 0 To .Rows.Count - 1
If .Rows(i)("name") & "" <> "" Then
temp &= .Rows(i)("name") & ", "
End If
Next
End With
temp = temp.Trim(",")
testing &= "&Name=" & temp & vbCrLf
With this is get a comma in the end of the string. But if I do
temp = temp.Trim.Trim(",")
all commas are deleted.
How do I keep all commas and only delete the last one?
temp = temp.TrimEnd(CChar(","))
That will do it and I think it is the easiest way.
temp = temp.Trim().Substring(0, temp.Length - 1)
or
temp = temp.Trim().Remove(temp.Length - 1)
You can avoid the Trim/extra character if you set a delimiter within the loop
Dim delimiter as string = ""
For i As Integer = 0 To .Rows.Count - 1
If .Rows(i)("name") & "" <> "" Then
temp &= delimiter & .Rows(i)("name")
delimiter = ","
End If
Next
The Trim() function has a Char() (static array of characters) parameter on it, you don't need to pass a Char explicitly.
' VB.Net Version
", Hello ^_^ ^_^ ,,, , ,, ,, ,".Trim({" "c, ","c})
//C# version
", Hello ^_^ ^_^ ,,, , ,, ,, ,".Trim({' ', ','})
Would produce the output
"Hello ^_^ ^_^"
The multi-parameter .Trim() removes the specified characters from both the beginning and the end of the string. If you want to only trim out the beginning or the end, use .TrimStart() or .TrimEnd() respectively.
This works:
dim AlarmStr as string="aaa , bbb , ccc , "
AlarmStr = AlarmStr.Remove(AlarmStr.LastIndexOf(","))
Check to see if the loop is done before adding the last comma. #cleaner
While dr.Read
For c As Integer = 0 To dr.FieldCount - 1
s.Append(dr(c).ToString.Trim)
'This line checks if the loop is ending before adding a comma
If (c < dr.FieldCount - 1) Then s.Append(",")
Next
s.Append(vbCrLf)
End While
I wonder, that based on the OP's code sample, nobody suggested to use String.Join or build an output with StringBuilder
Dim names As New List(Of String)
With ad.Tables(2)
For i As Integer = 0 To .Rows.Count - 1
' DataRow("columnName").ToString() returns value or empty string if value is DbNull
If .Rows(i)("name").ToString() <> "" Then
names.Add(.Rows(i)("name"))
End If
Next
End With
Dim temp As String = String.Join(", ", names)
testing &= "&Name=" & temp & vbCrLf
With LINQ and DataRow extension method .Field(Of T) code will look simpler
Dim names = ad.Tables(2).
AsEnumerable().
Select(row => row.Field<String>("name")).
Where(name => String.IsNullOrEmpty(name) = False)
Dim temp As String = String.Join(", ", names)
testing &= "&Name=" & temp & vbCrLf
If you go further you can(should) use StringBuilder for building a string and use Aggregate method to build string from the collection
I know that a little bit odd, but I found this works for me:
Visual Basic:
Every time: mystring = "today, I love go out , , , ," (less than five commas and spaces)
mystring.Trim.TrimEnd(",").Trim.TrimEnd(",").Trim.TrimEnd(",").Trim.TrimEnd(",").Trim.TrimEnd(",")
temp = temp.TrimEnd()
this trims the trailing spaces from a string.