I want to display the first and last characters of any given string entered into a textbox. The strings can be of any length as the user wants (as long as it is one word) I would like to be able to do something like this... "william = will and iam" or "Celtic = Cel and tic"
I understand I would have to split or divide the string. How would I go about doing this? Any help is appreciated, thanks.
EDIT:
Thanks for your help once again guys, this is how the code ended up!
Dim strInput = txtString.Text
Dim halflength = strInput.Length / 2
Dim firsthalf = strInput.Substring(0, halflength)
Dim secondhalf = strInput.Substring(halflength)
Dim strResults = firsthalf
Dim secondResult = secondhalf
MessageBox.Show(firsthalf)
MessageBox.Show(secondhalf)
MessageBox.Show("First half of string contains... " & " " & strResults.Length.ToString & " characters", "Character Count")
MessageBox.Show("Second half of string contains... " & " " & secondResult.Length.ToString & " characters", "Character Count")
EDIT:
Also meant to mention my current incorrect code.
Dim strInput As String
Dim strLength As String
Dim strResults As String
strInput = txtString.Text
strLength = strInput.Length / 2
strResults = txtString.Text
MessageBox.Show(strInput.Length.ToString, "Length of characters")
MessageBox.Show(strLength.ToString)
MessageBox.Show(strResults.Substring(0, 3))
String.Substring and String.Length should give you everything you need to get started on this.
Seeing your existing code will make this easier. Let's walk through what we have now.
Let's assume we have just a plain, simple string like this instead of a textbox for the sake of making things easier:
Dim txtString = "Hello World"
Now, in order to split the length of the string in half; we need to get the length. The `Length property will give is that, and then divide it by two.
Dim halfLength = txtString.Length \ 2
This will perform integer division; so any remaining decimal is truncated.
Now we know where the middle of the string is. We can now use String.Substring to carve out a peice of the string by index. Substring takes two parameters, the index where to start the string, and number of characters to take. There is a second overload that takes the index to start at and consumes till the end of the string. Indexes are zero based. So for example, if we wanted to start at the beginning of the string, we'd use zero. If we wanted to skip the first character, we'd use one.
For the first half of the string, we don't want to skip any characters, so we'll use zero. The number of characters we want is half length of the string, so we pass in halfLength:
Dim firstHalf = txtString.Substring(0, halfLength)
For the second half, we want to start in the middle of the string, and consume characters till the end, so we'll use the other overload:
Dim secondHalf = txtString.Substring(halfLength)
You now have your string split in half.
The final result looks like this:
Dim txtString = "Hello World"
Dim halfLength = txtString.Length \ 2
Dim firstHalf = txtString.Substring(0, halfLength)
Dim secondHalf = txtString.Substring(halfLength)
Assuming the rules are "each side is half the length with the left side taking precedence", you would use Substring and some simple division:
Dim str As String = "william"
Dim part1 As String = str.Substring(0, CInt(Math.Ceiling(str.Length / 2.0#)))
Dim part2 As String = str.Substring(part1.Length)
part1 & " and " & part2 'will and iam
Here's a demo.
My code displays first half and last half of any number of characters entered.
Declare Variable
Dim strResults As String
Fetch text from textbox
strResults = Textbox1.Text
Display the first half of the text
MessageBox.Show(strResults.Substring(0, strResults.Length / 2), "First Half Characters")
Display the last half of the text
MessageBox.Show(strResults.Substring(strResults.Length / 2), "Last Half Characters")
Full code:
Dim strResults As String
strResults = Textbox1.Text
MessageBox.Show(strResults.Substring(0, strResults.Length / 2), "First Half Characters")
MessageBox.Show(strResults.Substring(strResults.Length / 2), "Last Half Characters")
Related
I'm using this query in vb.net
Raw_data = Alltext_line.Substring(Alltext_line.IndexOf("R|1"))
and I want to increase R|1 to R|2, R|3 and so on using for loop.
I tried it many ways but getting error
string to double is invalid
any help will be appreciated
You must first extract the number from the string. If the text part ("R") is always separated from the number part by a "|", you can easily separated the two with Split:
Dim Alltext_line = "R|1"
Dim parts = Alltext_line.Split("|"c)
parts is a string array. If this results in two parts, the string has the expected shape and we can try to convert the second part to a number, increase it and then re-create the string using the increased number
Dim n As Integer
If parts.Length = 2 AndAlso Integer.TryParse(parts(1), n) Then
Alltext_line = parts(0) & "|" & (n + 1)
End If
Note that the c in "|"c denotes a Char constant in VB.
An alternate solution that takes advantage of the String type defined as an Array of Chars.
I'm using string.Concat() to patch together the resulting IEnumerable(Of Char) and CInt() to convert the string to an Integer and sum 1 to its value.
Raw_data = "R|151"
Dim Result As String = Raw_data.Substring(0, 2) & (CInt(String.Concat(Raw_data.Skip(2))) + 1).ToString
This, of course, supposes that the source string is directly convertible to an Integer type.
If a value check is instead required, you can use Integer.TryParse() to perform the validation:
Dim ValuePart As String = Raw_data.Substring(2)
Dim Value As Integer = 0
If Integer.TryParse(ValuePart, Value) Then
Raw_data = Raw_data.Substring(0, 2) & (Value + 1).ToString
End If
If the left part can be variable (in size or content), the answer provided by Olivier Jacot-Descombes is covering this scenario already.
Sub IncrVal()
Dim s = "R|1"
For x% = 1 To 10
s = Regex.Replace(s, "[0-9]+", Function(m) Integer.Parse(m.Value) + 1)
Next
End Sub
I have a code like this :
Dim mincatval As String
Dim strarr() As String = dr1(0).ToString().Split(New Char() {"-"c})
Dim i As String
i = (Integer.Parse(strarr(0)) + 1)
mincatval = i
my dr(1) value is L1 i want to add 1,so i want the out put L2,but i am getting error like this :Input string was not in a correct format.
Supposing that strarr(0) is the word "L1" and you want it to become "L2" then you need to isolate the numeric part from the text part and then rebuild the string taking the first part of strarr and the incremented value
Dim mincatval As String
Dim strarr() As String = dr1(0).ToString().Split(New Char() {"-"c})
Dim i As String
Dim itShouldBeAnumber = strarr(0).Substring(1)
if Int32.TryParse(itShouldBeAnumber, i) Then
mincatval = strarr(0).Substring(0,1) & (i + 1)
else
MessageBox.Show("Not a valid number from position 1 of " & strarr(0))
End if
Of course this solution assumes that your string is Always composed of an initial letter followed by a numeric value that could be interpreted as an integer
I have an assignment in VB.NET that I'm stuck with at the moment. Would love some help.
The question is this: You enter random characters into a textbox, for example 12ab3c4d5efgh, and at the click of a button, it must sort the characters in the textbox into 2 separate Labels, depending on whether or not the 'character' is a number or letter. So, continuing the example, Label1 must show '12345' and Label 2 must show 'abcdefgh'. I hope I made myself clear enough.
I was asked to use the Val() function but I really have no clue. Could someone please help? :D
This creates one string with the digits and one with the letters. Characters that are not digits or letters are ignored.
Dim chars As String = "12ab3c4d5efgh"
Dim nums As String = chars.Where(Function(c) Char.IsDigit(c)).ToArray
Dim lets As String = chars.Where(Function(c) Char.IsLetter(c)).ToArray
If you have to use Val() something like this would do. But be careful: Val("0") also returns 0.
Dim numbers As String = String.Empty
Dim letters As String = String.Empty
Dim sourceString As String = "12ab3c4d50efgh"
For Each c As Char In sourceString
If Val(c) = 0 And c <> "0" Then letters &= c Else numbers &= c
Next
Console.WriteLine("Numbers: " & numbers)
Console.WriteLine("Letters: " & letters)
Console.ReadKey()
I am trying to take a string in a rich text box and replace them with a different string.
Now how this should work is that if two same characters are entered into the text box
e.g tt the "tt" will be replaced with "Ǿt" , it adds back one of the t's to the replaced string. Only the most recently entered string is manipulated if two same characters are entered .
I read the LAST string that is in the RichTextBox by using this method
Dim laststring As String = RichTextBox1.Text.Split(" ").Last
'hitting space bar breaks the operation so if i enter t t there will be no replacement
this is the replacement method which I use , it works correctly .
if laststring = "tt"
RichTextBox1 .Text = RichTextBox1 .Text.Replace("tt", "Ǿt")
This method is inefficient because i need to check id there are double letters for all letters and if i was to use this method it would tavke up a lot of code .
how can I accomplish this using a shorter method??
You need to put the if then section in a loop.
Dim holdstring As String
Dim doubleinstance() As String = {"bb", "tt", "uu"} ' array
Dim curstring As String = RichTextBox1.Text.Split(" ").Last
For Each item As String In doubleinstance
If RichTextBox1.Text.EndsWith(item) Then
holdstring = RichTextBox1.Text.Split(" ").Last.Length - 1 ' change to subtract 1 character from doubleinstance
RichTextBox1.Text = RichTextBox1.Text.Replace(curstring, "Ǿt" & holdstring)
MsgBox(curstring)
End If
Next item
Here's a bit of code to get you in the right direction...
There are a couple of variations of .Find, but you probably want to look at the .Select method.
With RichTextBox1
.Find("Don")
.SelectedText = "Mr. Awesome"
End With
Here is a way I came up with
Dim holdstring As String
Dim doubleinstance() As String = {"bb", "tt", "uu"} ' array
Dim curstring As String = RichTextBox1.Text.Split(" ").Last
If curstring = doubleinstance(0) And RichTextBox1.Text.EndsWith(doubleinstance(0)) Then
holdstring = RichTextBox1.Text.Split(" ").Last.Length - 1 ' change to subtract 1 character from doubleinstance
RichTextBox1.Text = RichTextBox1.Text.Replace(curstring, "Ǿt" + holdstring)
MsgBox(curstring)
End If
where i have doubleinstance(0) how do i get the if statement to not only check a single index but all of the index from 0 to 2 in this example ?
I have a strange problem here. In my code, variable b string, has the value "Test Test Test". This value we can see while debugging the variable as well as in the text visualizer.
Now the problem is, if I show the same string using Messagebox, the value is just "Test". What can I do here to get the complete value.
I am converting from an ebcdic encoded bytes to corresponding utf8 string and doing the above operation. Any thoughts. below is my sample code.
Dim hex As String = "e385a2a300000000e385a2a3000000e385a2a3"
Dim raw As Byte() = New Byte((hex.Length / 2) - 1) {}
Dim i As Integer
For i = 0 To raw.Length - 1
raw(i) = Convert.ToByte(hex.Substring((i * 2), 2), &H10)
Next i
Dim w As String = System.Text.Encoding.GetEncoding(37).GetString(raw)
Dim raw1 As Byte() = Encoding.UTF8.GetBytes(w)
Dim b As String = Encoding.UTF8.GetString(raw1)
MessageBox.Show(b)
Look at the byte array. You have 4 ASCII 0's after each "Test". ASCII character code 0 corresponds to nul, which is a string termination sequence. If you want spaces instead of nulls there...
Dim b As String = Encoding.UTF8.GetString(raw1).Replace(Chr(0), " ")
It is possible that the string "b" might contains some control character.
To test a control char in string.
For Each p As Char In b
MsgBox(p & " " & Char.IsControl(p) & " " & AscW(p))
Next
Use String#Replace to replace control chars.
b = b.Replace(ChrW(0), " ")
MsgBox(b)