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
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.
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
Related
working on a project where I input a text file and write a sentence in an "English" text box and with a click of a button the program translates it into "textese" type of language. It will only change words listed in the text file and ignore everything else. Below is the code I have, and wondering what is going wrong, it fails to run and highlights my If Then statement, so I think my issue is there but not sure what. A few examples of what is in the text file and how they are ordered / separated by a comma.
anyone,ne1
are,r
ate,8
band,b&
be,b
before,b4
busy,bz
computer,puter
Public Class frmTextese
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")
Dim english As Integer = 0
Dim englishSentence As String = txtEnglish.Text
Dim result() As String = englishSentence.Split(" "c)
Do While english < (result.Length - 1)
Dim line As String
Dim data() As String
Dim englishArray As String
Dim texteseArray As String
For i As Integer = 0 To (inputData.Length - 1)
line = inputData(i)
data = line.Split(","c)
englishArray = line.Split(","c)(0)
texteseArray = line.Split(","c)(1)
If result(i).StartsWith(englishArray(i)) Then
If englishArray(i).Equals(texteseArray(i)) Then
result(i) = texteseArray(i)
End If
End If
txtTextese.Text = result(i)
Next
Loop
End Sub
End Class
You only need to compare result(i) against englishArray. Also, your while loop was an endless loop. When searching the textese array, once you find a match, you can quit searching and go on to the next english word. Finally, you should take care to use variable names that describe their purpose.
Look at this code (untested). I made the string comparison case insensitive, but that is not required.
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
'Get all the textese definitions
Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")
Dim english As Integer = 0
Dim englishSentence As String = txtEnglish.Text
Dim result() As String = englishSentence.Split(" "c)
Do While english < (result.Length - 1)
Dim line As String
Dim data() As String
Dim englishArray As String
Dim texteseArray As String
For i As Integer = 0 To (inputData.Length - 1)
'Split the textese entry into two parts
line = inputData(i)
data = line.Split(","c)
englishArray = data(0)
texteseArray = data(1)
'Compare the word in the english sentence against the word in the textese array
'using a case insensitive comparison (not required)
If result(i).Equals(englishArray, StringComparison.CurrentCultureIgnoreCase) Then
'Replace the word in the sentence with its textese version
result(i) = texteseArray
'If we found the word, there is no need to continue searching, so
'skip to the next word in the english sentence
Exit For
End If
Next
'Increment the loop counter to avoid an endless loop
english = english + 1
Loop
'Take the elements of the result array and join them together, separated by spaces
txtTextese.Text = String.Join(" ", result)
End Sub
I want to extract same word from a paragraph. My paragraph is in richtextbox1 and the words to be extracted are given in an array. My code is as below:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim A(1) As Char
A(0) = " "
A(1) = ","
Dim B As String = RichTextBox1.Text
Dim x As String() = Nothing
Dim F As Array = {"SMUGGLING", "CROSSING", "INFILTRATION"}
x = B.Split(A)
For Each F In x
Label1.Text += F.Contains(x) & ControlChars.NewLine
Next
End Sub
I think you have got your for each loop mixed up a bit.
With the below code, having the F.contains here, will output as either true or false in the label rather than the word itself.
Label1.Text += F.Contains(x) & ControlChars.NewLine
I don't think F.contains will work while using an array, as .Contains is not a member of System.Array.
I would consider using generic lists instead.
Here is an example I have made using generic lists instead.
Dim A(1) As Char
A(0) = CChar(" ")
A(1) = CChar(",")
Dim B As String = RichTextBox1.Text
Dim x As String() = Nothing
Dim F As List(Of String) = New List(Of String)
F.Add("SMUGGLING")
F.Add("CROSSING")
F.Add("INFILTRATION")
x = B.Split(A)
For Each word In x
If F.Contains(UCase(word)) Then
Label1.Text += word & ControlChars.NewLine
End If
Next
I have reworded your for each loop so that the the F.contains is an If statement and then adds it to the label if it returns true. Also in the for each loop you don't really want to be using for every F in x as it doesnt make sense and you are already using F. So i changed it to for each word in x.
Hope this helps :)
I'm working on a program that is supposed to perform the calculations for the Flesch Readability Index. The program is supposed to read in a text file "Project7.txt", it's then supposed to display the text in a multi-line text box and perform the following calculations:
Count the number of words in the file.
Count the number of syllables in the file.
Count the number of sentences in the file (a sentence can be ended by a ".", "?", "!", or ":"
The program is then supposed to plug the values into the following formula and display the result in a label (label1).
206.835-85.6*(Number of syllables/Number of words) - 1.015*(Number of words/Number of sentences)
Here is the code I have written so far.
Option Strict On
Imports System.IO
Public Class Form1
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
Dim open As New OpenFileDialog
open.Filter = "text files |project7.txt|All file |*.*"
open.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
If open.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim selectedFileName As String = System.IO.Path.GetFileName(open.FileName)
If selectedFileName.ToLower = "project7.txt" Then
Dim doc As String = ""
Dim line As String
Using reader As New StreamReader(open.OpenFile)
While Not reader.EndOfStream
doc += reader.ReadLine
Console.WriteLine(line)
End While
Dim text = File.ReadAllText("Project7.txt")
Dim words = text.Split(" "c)
Dim wordCount = words.Length
Dim separators As Char() = {"."c, "!"c, "?"c, ":"c}
Dim sentences = text.Split(separators, StringSplitOptions.RemoveEmptyEntries)
Dim sentenceCount = sentences.Length
End Using
Else
MessageBox.Show("You cannot use that file!")
End If
End If
End Sub
Function CountSyllables(word As String) As Integer
word = word.ToLower()
Dim dipthongs = {"oo", "ou", "ie", "oi", "ea", "ee", _
"eu", "ai", "ua", "ue", "au", "io"}
For Each dipthong In dipthongs
word = word.Replace(dipthong, dipthong(0))
Next
Dim vowels = "aeiou"
Dim vowelCount = 0
For Each c In word
If vowels.IndexOf(c) >= 0 Then vowelCount += 1
Next
Return vowelCount
End Function
End Class
Any suggestions are appreciated. Thanks in advance for the help.
Is the code always reporting one more sentence than there actually is?
If so take a look at this from the String.Split method MSDN docs:
When the Split function encounters two delimiters in a row, or a
delimiter at the beginning or end of the string, it interprets them as
surrounding an empty string ("")...
I'm sure your last sentence ends with your sentence delimiter so what's happening is your assignment to sentences is getting an extra, empty array element. See for yourself by breakpointing the line after your assignment and hovering your mouse over sentences. Examine the contents of the array.
The fix is to call Split with the option to remove empty array values. To do that though you'll need to call the Split overload that takes an array of Char for the delimiters:
Replace this line:
Dim sentences = text.Split("."c, "!"c, "?"c, ":"c)
With this:
Dim separators As Char() = {"."c, "!"c, "?"c, ":"c}
Dim sentences = text.Split(separators, StringSplitOptions.RemoveEmptyEntries)
And you should be good.
How do I filter/extract strings?
I have converted a PDF file into String using itextsharp and I have the text displayed into a Richtextbox1.
However there are too many irrelevant text that I don't need in the Richtextbox.
Is there a way I can display the text I want based on keywords, the entire length of the text.
Example of text that is displayed in textrichbox1 after conversation of PDF to text:
**774**
**Bos00232940
Bos00320491
Das1234
Das3216**
RAGE*
So the keywords would be "Bos", "Das", "774". and the new text that would be displayed in the richtextbox1 is shown below, instead of the entire text above.
*Bos00232940
Bos00320491
Das1234
Das3216
774*
Here is what I have so far. But it doesn't work it still displays the entire PDF in the richtextbox.
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pdffilename As String
pdffilename = TextBox1.Text
Dim filepath = "c:\temp\" & TextBox1.Text & ".pdf"
Dim thetext As String
thetext = GetTextFromPDF(filepath)
Dim lines() As String = System.Text.RegularExpressions.Regex.Split(thetext, Environment.NewLine)
Dim keywords As New List(Of String)
keywords.Add("Bos")
keywords.Add("Das")
keywords.Add("774")
Dim newTextLines As New List(Of String)
For Each line As String In lines
For Each keyw As String In thetext
If line.Contains(keyw) Then
newTextLines.Add(line)
Exit For
End If
Next
Next
RichTextBox1.Text = String.Join(Environment.NewLine, newTextLines.ToArray)
End Sub
SOLUTION
Thanks everyone for your help. Below is the code that worked and did exactly what I wanted it to do.
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pdffilename As String
pdffilename = TextBox1.Text
Dim filepath = "c:\temp\" & TextBox1.Text & ".pdf"
Dim thetext As String
thetext = GetTextFromPDF(filepath)
Dim re As New Regex("[\t ](?<w>((774)|(Bos)|(Das))[a-z0-9]*)[\t ]", RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase Or RegexOptions.Compiled)
Dim Lines() As String = {thetext}
Dim words As New List(Of String)
For Each s As String In Lines
Dim mc As MatchCollection = re.Matches(s)
For Each m As Match In mc
words.Add(m.Groups("w").Value)
Next
Next
RichTextBox1.Text = String.Join(Environment.NewLine, words.ToArray)
End Sub
For Each Word As String In thetext.Split(" ")
For Each key As String In keywords
If Word.StartsWith(key) Then
newTextLines.Add(Word)
Continue For
End If
Next
Next
or using LINQ:
Dim q = From word In thetext.Split(" ")
Where keywords.Any(Function(s) word.StartsWith(s))
Select word
RichTextBox1.Text = String.Join(Environment.NewLine, q.ToArray())
If don't know the keywords in advance but know in which context they occur, you can find them with a Regex expression. Two very handy Regex expressions allow you to find occurences succeeding or preceeding another:
(?<=prefix)find finds a pattern that follows another.
find(?=suffix) finds a pattern that comes before another.
If your number keyword (774) always preceeds " SIZE" you can find it like this: \w+(?=\sSIZE).
If the other keywords are always between "EX " and " DETAILS" you can find them like this: (?<=EX\s)(\w+\s)+(?=DETAILS).
You can put the whole thing together like this: \w+(?=\sSIZE)|(?<=EX\s)(\w+\s)+(?=DETAILS).
The disadvantage is that the keywords between "EX " and "DETAILS" will be returned as one match. But you can split the matches afterwards as in:
Const input As String = "2 3 3 4 4 A A B B SHEET 1 OF 1 774 SIZE SCALE 24.000-47.999 12.000-23.999 CON BAG WIRE 90in. EX Bos00232940 Bos00320491 Das1234 Das3216 DETAILS 1 2 RAGE"
Dim matches = Regex.Matches(input, "\w+(?=\sSIZE)|(?<=EX\s)(\w+\s)+(?=DETAILS)")
For Each m As Match In matches
Dim words = m.Value.Split(" "c)
For Each word As String In words
If word.Length > 0 Then ' Suppress the last empty word.
Console.WriteLine(word)
End If
Next
Next
Output:
774
Bos00232940
Bos00320491
Das1234
Das3216
How to do it with regular expression...
Dim re As New Regex("[\t ](?<w>((774)|(Bos)|(Das))[a-z0-9]*)[\t ]", RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase Or RegexOptions.Compiled)
Private Sub test()
Dim Lines() As String = {"2 3 3 4 4 A A B B SHEET 1 OF 1 774 SIZE SCALE 24.000-47.999 12.000-23.999 CON BAG WIRE 90in. EX Bos00232940 Bos00320491 Das1234 Das3216 DETAILS 1 2 RAGE"}
Dim words As New List(Of String)
For Each s As String In Lines
Dim mc As MatchCollection = re.Matches(s)
For Each m As Match In mc
words.Add(m.Groups("w").Value)
Next
Next
End Sub
Regex break down...
[\t ] Single tab or space (there is an alternative for whitespace too)
(?<w> Start of capture group called "w" This the the text returned later in the "m.Groups"
((774)|(Bos)|(Das)) one of the 3 blobs of text
[a-z0-9]* any a-z or 0-9 character, * = any number of them
) End of Capture group "w" from above.
MVC 3. Vb.net. Part of my app generates PDF files using Itextsharp. Some strings are too long to go onto the background image correctly. So I basically need to split this string when its over 26 characters long and when it splits it cant split in the middle of a word. from there I will use newline to add the string to the right to the next line... Any ideas that might point me in the right direction.. I did start bulding the function that I will pass the string into test for length and then pass back the string after it finishes but I am stummped after that..
Private Function stringLength(ByVal _string As String) As String
If _string.Length < 26 Then
_string.Split(
End If
End Function
I'm sure there's a million different ways to do this.
You basically need to get all of your words split by the space into a list. After that, you just need to keep checking if the current word plus a space plus the next word reach your threshold or not, and if it does, you move to the next line. Once you have all of your lines, then you rejoin the list into a single string again.
Private Function LimitWidth(ByVal text As String, ByVal maxCharacters As Integer) As String
Dim words As List(Of String) = text.Split(" "c).ToList()
If text.Length < maxCharacters OrElse words.Count = 1 Then
Return text
Else
Dim lines As New List(Of String)
Dim currentLine As String = words(0)
For i As Integer = 1 To words.Count - 1
If (currentLine & " " & words(i)).Length > maxCharacters Then
lines.Add(currentLine)
currentLine = words(i)
If i = words.Count - 1 Then
lines.Add(currentLine)
End If
Else
If i = words.Count - 1 Then
lines.Add(currentLine & " " & words(i))
End If
currentLine &= " " & words(i)
End If
Next
Return String.Join(Environment.NewLine, lines.ToArray())
End If
End Function
To Test:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
MessageBox.Show(LimitWidth("This is a really long sentence " & _
"meant to demonstrate how to split " & _
"the words into a confined character length.", 26))
End Sub
It sounds like you are asking for a word wrap function.
Since I feel that it's better to answer in a way that promotes learning than to just give answers, I have for you a link that walks you through the process of using Test Driven Development (TDD) to solve this problem. It just so happens that the word wrap problem is a popular coding kata, and Robert C. Martin wrote a somewhat silly fictional story about a developer being taught how to use TDD to solve the word wrap kata.
The code examples are in Java, but it should be trivial to read and translate.
http://thecleancoder.blogspot.com/2010/10/craftsman-62-dark-path.html
The goofy bits are skip-able. Just jump down to the sentences right before the first code snippet.
I would add to it handling of multiline input text with following:
Private Function LimitWidth(ByVal text As String, ByVal maxCharacters As Integer, SplitSign As String) As String
Dim Output As String = ""
Dim OrgLines As List(Of String) = text.Split(Environment.NewLine).ToList()
For x As Integer = 1 To OrgLines.Count - 1
Dim words As List(Of String) = OrgLines(x).Split(" "c).ToList()
If text.Length < maxCharacters OrElse words.Count = 1 Then
Output += OrgLines(x)
Else
Dim lines As New List(Of String)
Dim currentLine As String = words(0)
For i As Integer = 1 To words.Count - 1
If (currentLine & " " & words(i)).Length > maxCharacters Then
lines.Add(currentLine)
currentLine = words(i)
If i = words.Count - 1 Then
lines.Add(currentLine)
End If
Else
If i = words.Count - 1 Then
lines.Add(currentLine & " " & words(i))
End If
currentLine &= " " & words(i)
End If
Next
Output += String.Join(SplitSign, lines.ToArray())
End If
Next
Return Output
End Function
use:
LimitWidth("your text", 80, Environment.NewLine)