I have long address as a string I want to add new line after 23 characters because in printing of a4 page the string it cuts down. So I want to create them in new line.
thank you for all your time here is how I solved it
If delivery_address.TextLength > 23 Then
delivery_address.Text = delivery_address.Text.Insert(23, Environment.NewLine)
End If
Related
how can i do a code to remove values <1 and values greater than> 80?
Textbox1.Lines
2
11
-1
82
73
11
12
13
14
15
Expected Output:
2
11
73
11
12
13
14
15
Why didn't work.
If TextBox1.Lines.Contains < 1 Then
TextBox1.Lines(0).String.Empty
ElseIf TextBox1.Lines.Contains > 80 Then
TextBox1.Lines(0).String.Empty
End if.
First, let us look up what the .Contains method expects. https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netframework-4.8
Public Function Contains (value As String) As Boolean Ah ha! It expects a string and returns True or False.
Now, let us look at the first line of your code
If TextBox1.Lines.Contains < 1 Then I don't see parenthesis containing a string so the method can't work. You are dealing with strings in a TextBox. .Lines are strings, .Contains expects a string. You can't compare an Integer to a string.
Code Explanation:
Use a StringBuilder. Every time you "change" a string the application must throw away the old string and create an entirely new string. When you are doing this at least 10 times you want to save all that thrashing about by using a StringBuilder which is mutable (changeable).
The string in the line must be converted to an Integer so we can compare it to another Integer. The AndAlso is a short circuit meaning if the first condition is False it doesn't bother checking the second condition. This is a smidge faster.
Private Sub OPCode()
Dim sb As New StringBuilder
For Each line In TextBox1.Lines
If CInt(line) > 1 AndAlso CInt(line) < 80 Then
sb.AppendLine(line)
End If
Next
TextBox2.Text = sb.ToString
End Sub
I had this select query where textbox value is my parameter.
The case is, when I type text in the textbox it will be the reference for my select query. For example below,
Suppose my textbox has already a content;
123 32 5
And then i will add some text for my select query
123 32 5 66
The return value will be "66". And if i add another text
123 32 55 66 6952
The return value will be "6952". And So on, regardless of the length of the text
My idea here is that I start selecting the values from the first space to the right of the textbox. But I have no idea on how to do it.
Please help. Thanks
Here's an answer to your question. Now if you have a question related to your code then you should post some code to go with it showing you have done a bit of work. I'll give you a hint. Strings.Split()
This gives the same result....the last item in your split
Dim xx As String = "438 2374 8437 46327"
Dim xstr As String = xx.Split(" ").Last
Dim xxstr() As String = xx.Split(" ")
MsgBox(xstr)
MsgBox(xxstr(xxstr.Count - 1))
Play with strings!
In my application, the users may need to add hexadecimals in the textbox during run time and I would like to get each hexadecimal value in an array by removing the spaces if there are any.
For example, the data in the textbox is "ff 00 ff 1a ff 00". I could able to get each hexadecimal as an array with the help of split. But, now I am trying to remove all the spaces as some users may enter the hexadecimals without giving the space. For example, the user my enter as "ff 00 ff1a ff 00". So, in this case the third array contains "ff1a", which is not true. I know how to remove the spaces, but cannot able to figure out the way to get each hexadecimal into an array.
'Used to split the strings based on the spaces, which is not useful
'Dim extraData As String() = Split(TextBox8.Text, " ")
'Used to remove the spaces
Dim myString = TextBox8.Text.Replace(" ", "")
Any suggestion will be appreciated.
You can use something like:
Dim input As String = TextBox1.Text
Dim list As New List(Of String)
input = input.Replace(" ", "")
For i As Integer = 0 To input.Length - 1 Step 2
list.Add(input.Substring(i, 2))
Next
Dim array = list.ToArray()
Note - The length of the input (after removing spaces) must be even. Otherwise, it may throw an exception.
If I convert the method from C# to VB.NET given in this answer:
Public Function StringToByteArray(hex As [String]) As Byte()
Dim NumberChars As Integer = hex.Length
Dim bytes As Byte() = New Byte(NumberChars / 2 - 1) {}
For i As Integer = 0 To NumberChars - 1 Step 2
bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
Next
Return bytes
End Function
Then you can call it like this:
StringToByteArray(YourInputString.Replace(" ", ""))
This will return an array of bytes and will also validate the input because it will thrown an exception if invalid characters are used in the input (like non-hex characters).
I know you already have accepted an answer, however, I'd like to suggest another alternative. I'm assuming, based on the context of your question and the example that you provided, that your intention is to parse a list of bytes, where each byte is represented by a two digit hexadecimal value.
If you use regex to find your matches, it will be more powerful. For instance, you can easily ignore all white-space characters (e.g. tabs, new-lines) and you can easily validate the data to make sure that only properly formatted bytes are in the string (i.e. 00 - FF). For instance, you could do something like this:
Dim bytes As New List(Of String)()
Dim m As Match = Regex.Match(TextBox1.Text, "^\s*((?<byte>[a-fA-F0-9]{2})\s*)*$")
If m.Success Then
Dim g As Group = m.Groups("byte")
If g.Success Then
For Each c As Capture In g.Captures
bytes.Add(c.Value)
Next
End If
Else
MessageBox.Show("Input is not properly formatted")
End If
However, if the idea is to parse the actual byte values, you can do so using Byte.Parse, like this:
Dim bytes As New List(Of Byte)()
Dim m As Match = Regex.Match(TextBox1.Text, "^\s*((?<byte>[a-fA-F0-9]{2})\s*)*$")
If m.Success Then
Dim g As Group = m.Groups("byte")
If g.Success Then
For Each c As Capture In g.Captures
bytes.Add(Byte.Parse(c.Value, NumberStyles.HexNumber))
Next
End If
Else
MessageBox.Show("Input is not properly formatted")
End If
Both of those examples use the same regex pattern to find all of the hexadecimal bytes in the string. The pattern it uses is:
^\s*((?<byte>[a-fA-F0-9]{2})\s*)*$
Here's the meaning of the regex pattern:
$ - The match must start at the beginning of the string (i.e. no invalid text can come before the bytes)
\s* - Any number of white-space characters can come before the first byte
\s - Any white-space character
* - Zero or more times
((?<byte>[a-fA-F0-9]{2})\s*)* - Any number of bytes, each separated by any number of white-space characters
( - Begins the group so that we can later apply a kleene star to the whole group (which means the whole group can repeat any number of times)
(?<byte> - Begins another, inner group which is named "byte". It is given a name so that we can easily reference the values captured by the group in the VB code.
[a-fA-F0-9]{2} - A two-digit hexadecimal byte (i.e. 00 - FF)
[a-fA-F0-9] - Any character between a and f or between A and F or between 0 and 9
{2} - There must be exactly two characters matching that specification
) - Ends the group named "byte". Notice that this named-group only captures the two actual hexadecimal digits. It does not capture any of the white-space between the bytes.
\s* - There can be any number of white-space characters after a byte
) - The ends outer group which includes a byte and all of the white-space that comes after it
* - The aforementioned kleene star meaning that the outer-group can repeat any number of times
$ - The match must end at the ending of the string (i.e. no invalid text can come after the bytes)
I have listbox has lines like this
12345678%32=5
4663578877fg
6883346899
,,,etc
How can I get the first two number of every line to be like this
12
46
68
BTW the listbox has only numbers in first of every line
Thanks!
myListBox.Items.Add("1234")
myListBox.Items.Add("567")
myListBox.Items.Add("890")
For position As Integer = 0 To myListBox.Items.Count - 1
myListBox.Items(position) = CStr(myListBox.Items(position)).Substring(0, 2)
Next
EDIT
We can use the RichTextBox.Lines property that returns a array of String. Each String in the array represents a line in the RichTextBox. If you want to store the first two digits in an array, you can try :
Dim intValues(Convert.ToInt32(RichTextBox1.Lines.Length)) As Integer 'Stores the digits
For position As Integer = 0 To Convert.ToInt32(RichTextBox1.Lines.Length) - 1
intValues(position) = Convert.ToInt32(RichTextBox1.Lines(position).Substring(0, 2))
Next
Dim phoneNumber As String = "077 47578 587(num)"
How do i strip the above string off every character which isnt a number. So only the numbers are left and then check to make sure it is 11 characters long?
dim number as string = Regex.Replace(phoneNumber,"[^0-9]","")
if number.length = 11 then
'valid number
else
'not valid
end if
You could loop on each character and check if it is a digit. While looping, check that the number of accepted characters (digits) is less than 11.
or
use a regex to remove all the alpha but you still will have to count at the end ....
Dim phoneNumber As String = "077 47578 587(num)"
Dim newPhoneNumber = String.Empty
For i = 0 To phoneNumber.Length - 1
If IsNumeric(phoneNumber(i)) Then
newPhoneNumber += phoneNumber(i)
End If
Next
Dim valid = newPhoneNumber.Length = 11
One possible solution is to treat the string as a character array, then retrieve only those characters with ascii codes within the paramaeters you define.
Ascii codes can be found at a resource such as: http://www.bolen.net/html/misc/ASCII-codes.html
Alternatively, you could use a regular expression to retrieve only the characters you want. My regex isn't so hot, so I can't give an example :)