New topic: 

Simple len/replace question.

<http://forums.realsoftware.com/viewtopic.php?t=34558>

         Page 1 of 1
   [ 12 posts ]                 Previous topic | Next topic          Author  
Message        waveuponwave          Post subject: Simple len/replace 
question.Posted: Thu Jul 08, 2010 9:20 pm                                 
Joined: Fri Jan 29, 2010 12:39 pm
Posts: 366
Location: Virginia U.S.A.                I am trying to use this code to take a 
dynamic long string and add an EndOfLine to it after a specific length point is 
reached. Can anyone give me some advice on how to finish the commented bit? 
Thanks.

Code:  dim i as integer
  dim strTest As String
  strTest = "Taking dog to the vet. Taking dog to the vet. Taking dog to the 
vet."
  i = len(strTest)
  
  If i > 45 Then
' Split strTest at the 45th character 
' Add EndOfLine at the 46th character place
' Put strTest string back together
End
      
_________________
RB2010r2 Pro on Win 7
I promise to help if I ever learn how to help myself.  
                             Top                 brisance          Post 
subject: Re: Simple len/replace question.Posted: Thu Jul 08, 2010 10:09 pm      
                   
Joined: Tue Oct 06, 2009 2:38 am
Posts: 306                Sounds like a hard-word warp. Create 2 TextAreas, and 
a Pushbutton. In the Pushbutton's Action event handler, enter this:

Code:dim start As Integer = 1
dim b As String

TextArea2.text = "" //clear TextArea2's text

while start < TextArea1.text.Len
  TextArea2.text = TextArea2.text + TextArea1.text.Mid( start, 45 ) + EndofLine
  start = start + 45
wend      
_________________
Mike Ash: Getting Answers
  
                             Top                 brisance          Post 
subject: Re: Simple len/replace question.Posted: Thu Jul 08, 2010 10:12 pm      
                   
Joined: Tue Oct 06, 2009 2:38 am
Posts: 306                Ugh, remove the superfluous "dim b As String" line. 
Working from two monitors and copy-pasting willy-nilly is bad.  Losing the 
ability to edit is worse.      
_________________
Mike Ash: Getting Answers
  
                             Top                 waveuponwave          Post 
subject: Re: Simple len/replace question.Posted: Thu Jul 08, 2010 10:20 pm      
                           
Joined: Fri Jan 29, 2010 12:39 pm
Posts: 366
Location: Virginia U.S.A.                Thanks I'll try it out.      
_________________
RB2010r2 Pro on Win 7
I promise to help if I ever learn how to help myself.  
                             Top                 npalardy          Post 
subject: Re: Simple len/replace question.Posted: Thu Jul 08, 2010 10:21 pm      
                   
Joined: Sat Dec 24, 2005 8:18 pm
Posts: 5974
Location: Canada, Alberta, Near Red Deer                No need to even use a 
textarea
strings would work just fine with much the same approach      
_________________
My web site Great White Software
RBLibrary.com REALbasic learning  
                             Top                waveuponwave          Post 
subject: Re: Simple len/replace question.Posted: Thu Jul 08, 2010 10:29 pm      
                           
Joined: Fri Jan 29, 2010 12:39 pm
Posts: 366
Location: Virginia U.S.A.                Yeah I know. I tried this.

Code:  If i > 45 Then
  strTest2 = right(strTest, intPos)
  strTest = left(strTest, intPos)
  strTest = strTest + EndOfLine
  strTest = strTest + strTest2
  End
  
  msgbox strTest
  msgbox str(i)

Almost...

I also tried the example, minus the textareas.

Code:  
  dim start As Integer
  
  while start < strTest.Len
  strTest2 = strTest + strTest.Mid( start, 45 ) + EndofLine 
  start = start + 45
  wend

Almost....

Neither quite have it.      
_________________
RB2010r2 Pro on Win 7
I promise to help if I ever learn how to help myself.  
                             Top                 brisance          Post 
subject: Re: Simple len/replace question.Posted: Thu Jul 08, 2010 10:39 pm      
                   
Joined: Tue Oct 06, 2009 2:38 am
Posts: 306                That's because you have to assign a proper 
termination case for the while loop.

Code:dim start As Integer = 1
dim end As Integer = strTest.Len

while start < end
  strTest2 = strTest + strTest.Mid( start, 45 ) + EndofLine 
  start = start + 45
wend

Yes, no TextAreas are needed, they were there to make it easier for others to 
follow and debug.      
_________________
Mike Ash: Getting Answers
  
                             Top                 waveuponwave          Post 
subject: Re: Simple len/replace question.Posted: Thu Jul 08, 2010 10:42 pm      
                           
Joined: Fri Jan 29, 2010 12:39 pm
Posts: 366
Location: Virginia U.S.A.                Thanks but that still does the same 
thing. It prints the last line twice?      
_________________
RB2010r2 Pro on Win 7
I promise to help if I ever learn how to help myself.  
                             Top                 waveuponwave          Post 
subject: Re: Simple len/replace question.Posted: Thu Jul 08, 2010 10:54 pm      
                           
Joined: Fri Jan 29, 2010 12:39 pm
Posts: 366
Location: Virginia U.S.A.                I think I have ti working like this:

Code:  dim start As Integer = 1
  dim strEnd As Integer = strTest.Len + 2
  
  while start < strEnd
  strTest2 = strTest + strTest.Mid( start, 45 ) + EndOfLine
  start = start + 1
  wend
  
  msgbox strTest2

Again thanks for the help.      
_________________
RB2010r2 Pro on Win 7
I promise to help if I ever learn how to help myself.  
                             Top                 waveuponwave          Post 
subject: Re: Simple len/replace question.Posted: Thu Jul 08, 2010 10:57 pm      
                           
Joined: Fri Jan 29, 2010 12:39 pm
Posts: 366
Location: Virginia U.S.A.                Thanks for your help brisance. I have 
tested the above code I posted of yours that I modified slightly and it works 
perfectly no matter how long the string grows. Great example!      
_________________
RB2010r2 Pro on Win 7
I promise to help if I ever learn how to help myself.  
                             Top                 waveuponwave          Post 
subject: Re: Simple len/replace question.Posted: Fri Jul 09, 2010 1:10 am       
                          
Joined: Fri Jan 29, 2010 12:39 pm
Posts: 366
Location: Virginia U.S.A.                Actually it didn't work. 
It looks fine in a msgbox but when I try to draw it in paint or dump it to a 
textarea it just repeats itself too many times and it doesn't look at all like 
the msgbox of it. Ugh. I know this isn't hard. I think I've been at it too many 
hours today.      
_________________
RB2010r2 Pro on Win 7
I promise to help if I ever learn how to help myself.  
                             Top                 waveuponwave          Post 
subject: Re: Simple len/replace question.Posted: Fri Jul 09, 2010 3:19 am       
                          
Joined: Fri Jan 29, 2010 12:39 pm
Posts: 366
Location: Virginia U.S.A.                I finally solved this using this 
method.

Code:  dim strTest As String
  dim strTest2 As String
  dim intPos As Integer
  strTest = "Take dog to the vet. Take cat to the vet. Take him to the vet."
  intPos = len(strTest)
  strTest2 = Mid(strTest,1,45) + EndOfLine + Mid(strTest,46,intPos)      
_________________
RB2010r2 Pro on Win 7
I promise to help if I ever learn how to help myself.  
                             Top            Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 12 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to