1

i m working with richtextbox which contains words and sentences...... but i need to convert long words into two parts or break long words like 8 characters or more then 8 characters words into tow parts.... but if word contains 9 characters i want to make two words one of 4 characters and other into 5 characters

im doing spell checker where i use permuduction and combination method which allows me to handle onley 8 chracters long word. so i want to break long words into parts

    ix = 0
    Label6.Text = 0

    For ix = Label6.Text To RichTextBox1.TextLength
        str = Mid(RichTextBox1.Text, ix, 1)

        If str = "آ" Or str = "ا" Or str = "ب" Or str = "ٻ" Or str = "ڀ" Or str = "ت" Or str = "ٿ" Or str = "ٽ" Or str = "ٺ" Then
            tillword = tillword & str

            Label6.Text = tillword.Length
        End If
    Next
Tony Abrams
  • 4,505
  • 3
  • 25
  • 32
  • Please turn on Option Strict. This is a 2 part process. First for the current project - In Solution Explorer double click My Project. Choose Compile on the left. In the Option Strict drop-down select ON. Second for future projects - Go to the Tools Menu -> Options -> Projects and Solutions -> VB Defaults. In the Option Strict drop-down select ON. This will save you from bugs at runtime. – Mary Oct 15 '19 at 10:52
  • 1
    I do not see that code you posted has anything to do with your desired result. Check String.Split() method to get a array of words that you can check for length in a loop and get .Substring() when length exceeds 8. Then a StringBuilder to rebuild the string. – Mary Oct 15 '19 at 11:38
  • So, when you find a word of 8+ chars, you split it in two parts (one of which (the second part?) can be `[half word] +1` long). When a word is, for example, 16+ chars, do you need to split it in 4+ parts? Back-tracking is faster, recursion is slower. But it depends on the length of the text. – Jimi Oct 15 '19 at 19:51

1 Answers1

2

Welcome to Stack Overflow!

What you want to do can be considered a special case of hyphenation, even if you aren't actually splitting the words at the end of the line and/or adding the customary dashes that indicate a hyphenation. Since you're just starting out here, I've gone and done as Mary suggested in a direct comment to your question -- used String.Split() to get a list of words, and an instance of StringBuilder to rebuild the string, as shown in the code below. This console app code should get you started, though still you'll have to work out a few of the issues yourself, or traverse some of the links at the bottom to produce better output (if needed):

Module Module1
    Sub Main()
        Dim test1 As String = "The Quick Brown Fox in Missisippi Jumped over the Lazy Dog in Tallahassee, 
and told his cousin in Texarkansas, saying the word antidisestablishmentarianism while doing it."
        Dim wrds As String() = test1.Split()
        Dim wrd As String
        Dim part1 As String
        Dim part2 As String
        Dim sb As Text.StringBuilder = New System.Text.StringBuilder()
        For Each wrd In wrds
            If wrd.Length > 8 Then
                part1 = wrd.Substring(0, 8)
                part2 = wrd.Substring(8)
                sb.Append(part1)
                sb.Append(" ")
                sb.Append(part2)
                sb.Append(" ")
            Else
                sb.Append(wrd)
                sb.Append(" ")
            End If
        Next
        Dim finalAnswer As String = sb.ToString()
        Console.WriteLine(finalAnswer)
        Console.WriteLine()
        Console.ReadLine()
    End Sub
End Module

and here is the output from the above program:

The Quick Brown Fox in Missisip pi Jumped over the Lazy Dog in Tallahas see, 
and told his cousin in Texarkan sas, saying the word antidise stablishmentarianism while doing it. 

Note that I've given you what you asked for, but not necessarily what you really want -- the last part of antidisestablishmentarianism is still too long, the strings, as split, are not pleasing to read. That's why it's really not that easy, and in my opinion, there's almost an art to it. Therefore...

Following is further information and some links for your to make sure it's what you want.

First, Here is one person's Pseudocode for a recursive solution close to what you want.

Second, from another developer (Bjarke Ebert),

Donald E. Knuth did a lot of work on the line breaking algorithm in his TeX typesetting system. This is arguably one of the best algorithms for line breaking - "best" in terms of visual appearance of result.

And third, some good suggestions from JasCav here:

Obviously, Donald Knuth's algorithms are excellent. Although there is not a C# implementation available, have you considered converting another implementation to C#? (For example, you could convert the Java implementation which is fairly close to C#.)

HTH.

  • It looks liske the OP needs to split words of 8+ chars in two parts (or more, depending on the length of the string?). You're taking the first 8 chars of a string when its length is 8+, leaving the rest untouched. E.g., `Mississippi` should become `Missi` + `ssippi`. It's not clear how `antidisestablishmentarianism` should be split. – Jimi Oct 15 '19 at 19:59
  • @Jimi - This is on purpose -- if he is a student, I don't want to do all of his homework for him ;-) Please see that I have already discussed this in "Note that I've given you what you asked for, but not necessarily what you really want..." The teacher always leaves some work for the student... ...so they can learn. – MicroservicesOnDDD Oct 16 '19 at 14:03
  • What in the OP's question (or previous questions) made you think about homework? Plus, how is this useful for other visitors? Note that the answer to this question is more complex that it apperas at first glance (just considering some languages' ligature shenanigans). – Jimi Oct 16 '19 at 15:07
  • @Jimi - You may be right -- I don't know much about ligatures. It sounds like you have enough to put up an answer of your own ;-) I'd actually like to see what you can do, and it sounds like it would be interesting. Thanks for challenging me, too (although I still think I did a good job. Let's see if you can show what I SHOULD have done.) – MicroservicesOnDDD Oct 17 '19 at 02:36