Stringsplit while word count reach specific characters Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Stringsplit while word count reach specific characters

13 Sep 2017, 08:25

is it possible to count total characters and stringsplit(a sentense in this example) it then:
1. insert a `n after 58th chars if total chars are greater than 58, the total count of first line should be less or equal 58, do not break a full word.
2. or there's no need to insert a `n, simply split the whole sentence to 2 strings, so we can call them and send input

example:

Code: Select all

This all works until the row is moved from its current row number above or below another name. ;94 characters
True

Code: Select all

This all works until the row is moved from its current row;`n New line 58 characters
 number above or below another name.
False

Code: Select all

This all works until the row is moved from its curren;`n New line 63 characters ("current" breaked)
t row number above or below another name.
User avatar
boiler
Posts: 17384
Joined: 21 Dec 2014, 02:44

Re: Stringsplit while word count reach specific characters

13 Sep 2017, 09:00

Starting with the 58th character, count backwards until you find a character that is a space and break it into two strings at that character using SubStr().
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Stringsplit while word count reach specific characters

13 Sep 2017, 09:06

boiler wrote:Starting with the 58th character, count backwards until you find a character that is a space and break it into two strings at that character using SubStr().
Thanks @boiler gonna figure it out
Last edited by ivill on 13 Sep 2017, 09:11, edited 1 time in total.
User avatar
boiler
Posts: 17384
Joined: 21 Dec 2014, 02:44

Re: Stringsplit while word count reach specific characters

13 Sep 2017, 09:09

You're welcome. Did you really mean to show the "off topic" emoji?
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Stringsplit while word count reach specific characters

13 Sep 2017, 09:12

:crazy:
boiler wrote:You're welcome. Did you really mean to show the "off topic" emoji?
sorry might be wrong click
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Stringsplit while word count reach specific characters  Topic is solved

13 Sep 2017, 10:38

Try this:

Code: Select all

Text := "This all works until the row is moved from its current row number above or below another name"
loop {   
   if ( substr(text, currChar := 60-A_Index, 1) = " " )
      break
}   
MsgBox % substr(Text, 1, currChar-1 ) "`n" substr(Text, currChar+1 )   
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Stringsplit while word count reach specific characters

13 Sep 2017, 10:40

See 'SEARCH FROM THE NTH CHARACTER BACKWARDS':
jeeswg's strings tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=32985
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Stringsplit while word count reach specific characters

13 Sep 2017, 12:13

Edit: You should take boiler's advice and try it yourself with substr first :thumbup:
Spoiler
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Stringsplit while word count reach specific characters

14 Sep 2017, 04:09

jeeswg wrote:See 'SEARCH FROM THE NTH CHARACTER BACKWARDS':
jeeswg's strings tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=32985
Thanks @jeeswg, i'll keep focusd on your codes, this function looks a bit difficult for me
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Stringsplit while word count reach specific characters

14 Sep 2017, 04:11

Helgef wrote:Edit: You should take boiler's advice and try it yourself with substr first :thumbup:
Spoiler
Thanks anyway @Helgef for your suggestion, i tried @Odlanir's code, and it does simple and works for me.
Last edited by ivill on 14 Sep 2017, 04:12, edited 1 time in total.
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Stringsplit while word count reach specific characters

14 Sep 2017, 04:12

Odlanir wrote:Try this:

Code: Select all

Text := "This all works until the row is moved from its current row number above or below another name"
loop {   
   if ( substr(text, currChar := 60-A_Index, 1) = " " )
      break
}   
MsgBox % substr(Text, 1, currChar-1 ) "`n" substr(Text, currChar+1 )  
Thanks, @Odlanir, it does simple and works for me!
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Stringsplit while word count reach specific characters

14 Sep 2017, 07:53

I've noticed that if the text length in less than 58 chars my code will split the last word.
This is the new code that should correct the issue.
It perform even on a really longer input string.
Hope this helps.
Regards.

Rinaldo.

Code: Select all

Text := "This all works until the row is moved from its current row number above or below another name or below another name 2 This all works until the row is moved from its current row number above or below another name or below another name 3 This all works until the row is moved from its current row number above or below another name or below another name 4 This all works until the row is moved from its current row number above or below another name or below another name END of Sentence This all works until the row is moved from its current row number above or below another name or below another name 2 This all works until the row is moved from its current row number above or below another name or below another name 3 This all works until the row is moved from its current row number above or below another name or below another name 4 This all works until the row is moved from its current row number above or below another name or below another name END of Sentence"

Loop
{   
   if StrLen(Text) <= 58 
      break
   posx := SplitTxt(Text) 
   oTxt .= Trim(substr(Text, 1, Posx )) "`n"
   text := substr(Text, Posx+2 )
}  
oTxt .= Text "`n"
MsgBox % oTxt
ExitApp

SplitTxt(txtIN) {
   loop {   
      if ( substr(txtIN, currChar := 59-A_Index, 1) = " " )
      return currChar - 1
   } 
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Stringsplit while word count reach specific characters

14 Sep 2017, 08:27

Odlanir wrote:Try this:

Code: Select all

Text := "This all works until the row is moved from its current row number above or below another name"
loop {   
   if ( substr(text, currChar := 60-A_Index, 1) = " " )
      break
}   
MsgBox % substr(Text, 1, currChar-1 ) "`n" substr(Text, currChar+1 ) 
i'm trying to add "else if" condition, if currChar <= 72 then there's no need to substr the %Text%, i tried alot, but none of the two codes in the below will working... could you/someone help me out? thanks!

Code: Select all

loop
{   
if ( substr(text, currChar := 72-A_Index, 1) = " " )
   break
}
if currChar > 72
{
say1 := substr(Text, 1, currChar-1 )
say2 := substr(Text, currChar+1 )
MsgBox % say1 "`n" "`n" say2
}
else
if currChar <= 72
{
MsgBox % Text
}

Code: Select all

loop
{   
   if ( substr(text, currChar := 72-A_Index, 1) = " " )
      break
}
say1 := substr(Text, 1, currChar-1 )
say2 := substr(Text, currChar+1 )
MsgBox % say1 "`n" "`n" say2
else
if ( substr(text, currChar <= 72, 1))
MsgBox % Text
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Stringsplit while word count reach specific characters

14 Sep 2017, 08:47

Code: Select all

Text := "This all works until the row is moved from its current row number above or below another name or below another name 2 This all works until the row is moved from its current row number above or below another name or below another name 3 This all works until the row is moved from its current row number above or below another name or below another name 4 This all works until the row is moved from its current row number above or below another name or below another name END of Sentence This all works until the row is moved from its current row number above or below another name or below another name 2 This all works until the row is moved from its current row number above or below another name or below another name 3 This all works until the row is moved from its current row number above or below another name or below another name 4 This all works until the row is moved from its current row number above or below another name or below another name END of Sentence"

len := 72 ; <=== choose any length for the output string

Loop
{   
   if StrLen(text) <= len 
      break
   posx := SplitTxt(Text) 
   oTxt .= Trim(substr(Text, 1, Posx )) "`n"
   text := substr(Text, Posx+2 )
}  
oTxt .= Text "`n"
MsgBox % oTxt
Clipboard := oTxt
ExitApp

SplitTxt(txtIN) {
   global len
   loop {   
      if ( substr(txtIN, currChar := len+1-A_Index, 1) = " " )
      return currChar - 1
   } 
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Stringsplit while word count reach specific characters

14 Sep 2017, 19:29

Odlanir wrote:

Code: Select all

Text := "This all works until the row is moved from its current row number above or below another name or below another name 2 This all works until the row is moved from its current row number above or below another name or below another name 3 This all works until the row is moved from its current row number above or below another name or below another name 4 This all works until the row is moved from its current row number above or below another name or below another name END of Sentence This all works until the row is moved from its current row number above or below another name or below another name 2 This all works until the row is moved from its current row number above or below another name or below another name 3 This all works until the row is moved from its current row number above or below another name or below another name 4 This all works until the row is moved from its current row number above or below another name or below another name END of Sentence"

len := 72 ; <=== choose any length for the output string

Loop
{   
   if StrLen(text) <= len 
      break
   posx := SplitTxt(Text) 
   oTxt .= Trim(substr(Text, 1, Posx )) "`n"
   text := substr(Text, Posx+2 )
}  
oTxt .= Text "`n"
MsgBox % oTxt
Clipboard := oTxt
ExitApp

SplitTxt(txtIN) {
   global len
   loop {   
      if ( substr(txtIN, currChar := len+1-A_Index, 1) = " " )
      return currChar - 1
   } 
}
sorry but you probably misunderstand my query... the split function works great, but missing else if condition...

Code: Select all

loop
{   
if ( substr(text, currChar := 72-A_Index, 1) = " " )
   break
}
say1 := substr(Text, 1, currChar-1 )
say2 := substr(Text, currChar+1 )
MsgBox % say1 "`n" "`n" say2
}
else ;expected else if, but not working
if currChar <= 72 ;expected else if, but not working
{ ;expected else if, but not working
MsgBox % Text ;expected else if, but not working
} ;expected else if, but not working
Return ;expected else if, but not working
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Stringsplit while word count reach specific characters

15 Sep 2017, 00:03

This may be useful:
Edit some text files, string length - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 69#p156069
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Stringsplit while word count reach specific characters

15 Sep 2017, 03:57

sorry but you probably misunderstand my query... the split function works great, but missing else if condition...
The last code I've posted should do what you ask.
If you look at the code the else condition is implicit.
If the input string is less then the length ( in your case 72 ), the loop does nothing and the string remainst unchanged.

Code: Select all

	...
	if StrLen(text) <= len 
	      break
	...
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], bobstoner289 and 199 guests