I have a code, which can change last letters of words to the dot. I need to how, how to change the code, so when I write some words, in output I will get them without first letter
, for ex: Input: Hello,how are you? OutPut: ello, ow re ou? Here is my code:
Sub New5
dim s, ns as String
dim r as String
s = inputbox("Input text")
r = "Inputed text:" & chr(9) & s & chr(13)
for i = 2 to len(s)
if mid(s,i,1)=" " then ns = ns "." else ns = ns mid(s,i-1,1)
next i
ns = ns "."
r = r & "Result of work:" & chr(9) & ns
MsgBox r
End Sub
CodePudding user response:
For VB6:
Private Sub Convert()
Dim strIn as string
Dim strA() As String
Dim strOut As String
Dim iX As Integer
strIn - "Hello, how are you?"
strA = Split(strIn, " ")
For iX = 0 To UBound(strA)
strA(iX) = Mid$(strA(iX), 2)
Next
strOut = Join(strA, " ")
End Sub
Incidentally your libreoffice tag is also inappropriate as LibreOffice doesn't use the same language as vb6 or vba.
CodePudding user response:
Sorry, just saw this was tagged vb6. This is a vb.net answer.
If you want to get rid of the first letter of each word, the first thing to do is get the words. String.Split
will return an array based on the split character you provide. In this case that character is a space. The small c
following the string tells the compiler that this is Char
.
Now we can loop through each word and cut off the first letter. I am storing the shortened words in a List(Of String)
. You can get rid of the first letter by using Substring
passing the start index. We want to start at the second letter so we pass 1
. Indexes start at 0
for the first letter.
Finally, use String.Join
to put everything back together.
Chr
, Len
, Mid
, and MsgBox
are all left overs from VB6. They work for backward compatibility but it is a good idea to learn the .net classes functionality.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
New5()
End Sub
Private Sub New5()
Dim input = InputBox("Input text")
Dim words = input.Split(" "c)
Dim ShortWords As New List(Of String)
For Each word In words
ShortWords.Add(word.Substring(1))
Next
Dim shortenedString = String.Join(" ", ShortWords)
MessageBox.Show(shortenedString)
End Sub