8 Replies - 638 Views - Last Post: 17 February 2013 - 10:26 AM Rate Topic: -----

#1 Jondeate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 03-February 13

Automatic New like if String > 65 Characters

Posted 16 February 2013 - 08:55 PM

Hello,

I am trying to display some text on screen.
If the string is too long, i want it to go to the next line.


I tried this code and it worked well,
(needing some help to simplify it, since it seems kind of redundant)
      

Public espacio As String = Environment.NewLine ' Espacio en string

  Dim i As Integer

        If Mensage.Length > 65 Then
            i = Mensage.Length / 65
            Select Case i
                Case 1
                    mensage2 = Mensage.Insert(65, " -" & espacio & "")
                Case 2
                    mensage2 = Mensage.Insert(65, " -" & espacio & "")
                    mensage2 = mensage2.Insert(130, " -" & espacio & "")
                Case 3
                    mensage2 = Mensage.Insert(65, " -" & espacio & "")
                    mensage2 = mensage2.Insert(130, " -" & espacio & "")
                    mensage2 = mensage2.Insert(195, " -" & espacio & "")
                Case 4
                    mensage2 = Mensage.Insert(65, " -" & espacio & "")
                    mensage2 = mensage2.Insert(130, " -" & espacio & "")
                    mensage2 = mensage2.Insert(195, " -" & espacio & "")
                    mensage2 = mensage2.Insert(260, " -" & espacio & "")
            End Select

        End If


but what if i want to insert the new line when there is a space available?
For example.

If this is a string.
"Hello world", and character number 65 is o, using my code it would result in

"Hell
o world"

How can i make it search to the next space so it results in
"Hello
world"

This post has been edited by Jondeate: 16 February 2013 - 08:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Automatic New like if String > 65 Characters

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9389
  • View blog
  • Posts: 35,262
  • Joined: 12-June 08

Re: Automatic New like if String > 65 Characters

Posted 16 February 2013 - 09:34 PM

Well.. a string is an array, right? Check the string's 65 position.. if it's a letter keep incrementing until you find a space then insert a new line. Repeat until you reach the end of the string.

Here's an example of breaking up a string into lengths of ten or the next nearest space.

        '-- the input string
        Dim foo As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas consequat, ligula quis adipiscing gravida, mauris ante posuere ante, non tempor eros mi vel ligula. "
        Console.WriteLine(foo) '-- lets see the input.
        Dim index As Int32 = 0 '-- our index variable to count off to ten.

        '-- repeat this loop until we go farter than the length of the string.
        While index < foo.Length
            index = 10 '-- i want every 10 characters

            '-- but I want to break at a space.. so if the tenth character isn't a space increase the index by one and test again.
            While index < foo.Length AndAlso foo(index) <> " "
                index += 1
            End While

            '-- show the output 
            Console.WriteLine(foo.Substring(0, index).Trim)
            '-- remove what we just printed off (shortening the array)
            foo = foo.Substring(index, foo.Length - index)

        End While
        '-- catch if we still have less than 10 characters left
        If foo.Length > 0 Then
            Console.WriteLine(foo.Trim)
        End If

Was This Post Helpful? 1
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Automatic New like if String > 65 Characters

Posted 16 February 2013 - 10:49 PM

I think modi123_1's code will make sthe strings too long. If I am not mistaken you want to break the line at the space preceding the 65th character, not the next space after the 65th character.

Have a look at SubString and LastIndexOf.

Basically, find the LastIndexOf(" ") in the first 65 characters of a line, and using the index found, replace the " " with vbNewLine. Then check the next line in the same way, starting at the index of the vbNewLine (+1) you just wrote into the string, for the next 65 characters, and repeat the process.
Was This Post Helpful? 1
  • +
  • -

#4 Jondeate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 03-February 13

Re: Automatic New like if String > 65 Characters

Posted 16 February 2013 - 11:07 PM

This is kind of confusing trying to implement it to my game.
Even thought i made it work on an empty form

thanks anyways

View Postlar3ry, on 16 February 2013 - 10:49 PM, said:

I think modi123_1's code will make sthe strings too long. If I am not mistaken you want to break the line at the space preceding the 65th character, not the next space after the 65th character.

Have a look at SubString and LastIndexOf.

Basically, find the LastIndexOf(" ") in the first 65 characters of a line, and using the index found, replace the " " with vbNewLine. Then check the next line in the same way, starting at the index of the vbNewLine (+1) you just wrote into the string, for the next 65 characters, and repeat the process.

Thanks, i will try this
Was This Post Helpful? 0
  • +
  • -

#5 Jondeate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 03-February 13

Re: Automatic New like if String > 65 Characters

Posted 16 February 2013 - 11:21 PM

View Postlar3ry, on 16 February 2013 - 10:49 PM, said:

I think modi123_1's code will make sthe strings too long. If I am not mistaken you want to break the line at the space preceding the 65th character, not the next space after the 65th character.

Have a look at SubString and LastIndexOf.

Basically, find the LastIndexOf(" ") in the first 65 characters of a line, and using the index found, replace the " " with vbNewLine. Then check the next line in the same way, starting at the index of the vbNewLine (+1) you just wrote into the string, for the next 65 characters, and repeat the process.


COuld you please show me an example of this?
I cant get it to work

I am kind of .. lost
 Dim mensage As String = "Hola como tu estas mi amorsote bello lindo preciosao"
        Dim ind As Integer
        ind = mensage.LastIndexOf(" ")
        mensage = mensage + mensage.Substring(0, ind)  &  Environment.NewLine
        Me.TextBox1.Text = mensage

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Automatic New like if String > 65 Characters

Posted 17 February 2013 - 03:19 AM

I might take a different approach, accumulating words and sentences :)

Here's a rough example:

    Sub Main()
        Dim sInput = "Here is a very long piece of text. Whataboutthislongsingleword? " &
            "We want to split it to (less than) 25 character (change to 65!) " &
            "sentences, as defined by the spaces that we find. Here is some more text to " &
            "fill-in, or fill-out. The end."

        Dim sOutput As New List(Of String)
        Dim sTemp As String = ""
        Dim counter As Integer = 0

        For Each word In sInput.Split(" ")
            If (counter + word.Length <= 25) Then
                sTemp &= word & " "
                counter += word.Length + 1
            Else
                sOutput.Add(sTemp.Trim(" "))
                sTemp = word & " "
                counter = word.Length + 1
            End If
        Next
        sOutput.Add(sTemp.Trim(" "))

        For Each sentence In sOutput
            Console.WriteLine(sentence)
        Next
        Console.ReadKey()

    End Sub

but, by all means, you may wish to continue with your current approach.

This post has been edited by andrewsw: 17 February 2013 - 03:27 AM

Was This Post Helpful? 1
  • +
  • -

#7 Jondeate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 03-February 13

Re: Automatic New like if String > 65 Characters

Posted 17 February 2013 - 07:19 AM

So basically, the code accumulates words ina list and then prints them out.
Am i right?
Was This Post Helpful? 0
  • +
  • -

#8 Jondeate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 03-February 13

Re: Automatic New like if String > 65 Characters

Posted 17 February 2013 - 10:19 AM

I made it work, thank you alot
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Automatic New like if String > 65 Characters

Posted 17 February 2013 - 10:26 AM

View PostJondeate, on 17 February 2013 - 10:19 AM, said:

I made it work, thank you alot

No worries! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1