Sign in to follow this  
Followers 0
msalperen

String Formatting Issue

13 posts in this topic

#1 ·  Posted (edited)

Hi,

I need advice on string formatting subject. I want a text output of a certain variable to be 64 bytes in length for each line and also each line should start with 10 bytes of space for instance. The string should not be stripped within a word when a line break is given - if possible. I poked StringFormat function help page but could not figure out how to do it.

Regards

Murat

Edited by msalperen

Share this post


Link to post
Share on other sites



would you like the 10 bytes of space included in the 64 bytes?


[size="2"] "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan[/size]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

would you like the 10 bytes of space included in the 64 bytes?

No, 64 bytes of data coming from the variable + 10 bytes of space from the left side for formatting purpose.

Edited by msalperen

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Func _Stringformat64($string)
    $array = StringRegExp($string,'(?ms)((?:.{0,64})\s)',3)
    for $i = 0 to UBound($array) - 1
        $array[$i] = '          ' & $array[$i]
    Next
    Return $array
EndFunc

how does that work?

Edited by nekkutta

[size="2"] "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan[/size]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Func _Stringformat64($string)
    $array = StringRegExp($string,'(?ms)((?:.{0,64})\s)',3)
    for $i = 0 to UBound($array) - 1
        $array[$i] = '          ' & $array[$i]
    Next
    Return $array
EndFunc

how does that work?

Hi, thanks for the instant reply. I'm going to try this one as soon as possible and let you know about the results. Thank you very much!

Update: It didn't work for me, it gives blank output.

Edited by msalperen

Share this post


Link to post
Share on other sites

Hi, thanks for the instant reply. I'm going to try this one as soon as possible and let you know about the results. Thank you very much!

Update: It didn't work for me, it gives blank output.

It returns it in an array, did you check for that?

Msgbox(0,'Number of 64 byte entries',Ubound(_Stringformat64($string)))

will give the number of segments


[size="2"] "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan[/size]

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

$array = StringRegExp($string,'(?ms)((?:.{0,64})\s)',3)

Will fail ... if the input string is lacking any blanks characters!

---

blank -> whitespace

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

$array = StringRegExp($string,'(?ms)((?:.{0,64})\s)',3)

Will fail ... if the input string is lacking any blanks characters!

I realize that, but I was coding simplistic to the OP specs, since they are wanting 64 byte segments, not separated in the middle of words, I am assuming there will be spaces, I originally tried
(?ms)((?:.{0,64})\b)
but the word boundary meta-character would separate in punctuation as well such as "that's" --> "that'","s" which would be unacceptable I believe. Edited by nekkutta

[size="2"] "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan[/size]

Share this post


Link to post
Share on other sites

@msalperen:

Post some example input text's for your request (+ example of intended output.)

Also including what you used when it did not work for you is also more usable.


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

The intended input is an ordinary text as shown below. I would be grateful if you send an example output (returning by array) assuming that the original text is an input of editbox (assume below text is shown by $dummy)

Intended input : $dummy = "1)DUMMY TEXT DUMY TEXT DUMMY TEXT. DUMMY TEXT DUMY TEXT DUMMY TEXT.DUMMY TEXT DUMY TEXT DUMMY TEXT..."

Intended Output should look like

Some Title Here  : 

          1)DUMMY TEXT DUMY TEXT DUMMY TEXT. DUMMY TEXT DUMY TEXT DUMMY TEXT.
          DUMMY TEXT DUMY TEXT DUMMY TEXT. DUMMY TEXT DUMY TEXT DUMMY TEXT.
          DUMMY TEXT DUMY TEXT DUMMY TEXT. DUMMY TEXT DUMY TEXT DUMMY TEXT. 
          DUMMY TEXT DUMY TEXT DUMMY TEXT. 
          2)DUMMY TEXT DUMY TEXT DUMMY TEXT. DUMMY TEXT DUMY TEXT DUMMY TEXT.
          DUMMY TEXT DUMY TEXT DUMMY TEXT. DUMMY TEXT DUMY TEXT DUMMY TEXT.
          DUMMY TEXT DUMY TEXT DUMMY TEXT. DUMMY TEXT DUMY TEXT DUMMY TEXT.
          DUMMY TEXT DUMY TEXT DUMMY TEXT.

Btw I know that this looks silly, but I can not send my code because that I'm at work now ;).

Edited by msalperen

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

nekkutta code works perfectly for what you asked.

#include <array.au3>
test__Stringformat()
Func test__Stringformat()
    Local $string = '1)Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque eu mauris ac nulla dignissim suscipit. Curabitur in mauris urna. Integer viverra vestibulum pretium. Nulla neque mauris, egestas id sodales a, bibendum ac mauris. Nulla nec leo magna, eget vestibulum tellus. Vivamus ultrices tincidunt molestie. Ut tristique dapibus leo, sit amet rutrum risus aliquam vitae. Pellentesque vitae ipsum mauris, vitae hendrerit sapien. Donec lectus nisl, bibendum quis elementum sed, adipiscing id nisi. Aliquam pellentesque, dui sit amet rutrum sodales, est sapien vulputate nisi, sed venenatis dolor tellus volutpat lorem. Sed fringilla scelerisque mi ac vulputate. Ut sem arcu, ornare vel ullamcorper non, aliquam at tortor. Aenean venenatis dictum pretium.'
    Local $result = _Stringformat($string)
    _ArrayDisplay($result)
#cs
--- --- 0 (array dump), $result:[13].
.   000 |St|           1)Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
.   001 |St|           Quisque eu mauris ac nulla dignissim suscipit. Curabitur in 
.   002 |St|           mauris urna. Integer viverra vestibulum pretium. Nulla neque 
.   003 |St|           mauris, egestas id sodales a, bibendum ac mauris. Nulla nec leo 
.   004 |St|           magna, eget vestibulum tellus. Vivamus ultrices tincidunt 
.   005 |St|           molestie. Ut tristique dapibus leo, sit amet rutrum risus 
.   006 |St|           aliquam vitae. Pellentesque vitae ipsum mauris, vitae hendrerit 
.   007 |St|           sapien. Donec lectus nisl, bibendum quis elementum sed, 
.   008 |St|           adipiscing id nisi. Aliquam pellentesque, dui sit amet rutrum 
.   009 |St|           sodales, est sapien vulputate nisi, sed venenatis dolor tellus 
.   010 |St|           volutpat lorem. Sed fringilla scelerisque mi ac vulputate. Ut 
.   011 |St|           sem arcu, ornare vel ullamcorper non, aliquam at tortor. Aenean 
.   012 |St|           venenatis dictum 
--- --- 0 [0,0]
#ce
EndFunc

Func _Stringformat($string, $iWidth = 64)
    $array = StringRegExp($string, '(?ms)((?:.{0,' & String($iWidth) & '})\s)', 3)
    For $i = 0 To UBound($array) - 1
        $array[$i] = '          ' & $array[$i]
    Next
    Return $array
EndFunc

---

If you get a blank output, your probably feeding it a empty string, or a string that has no whitespace characters.

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Ok, thank you very much. I missed the ArrayDisplay function which I have never been aware of. Thanks again and apologises for my lack of knowledge.

Kindly

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Just a tiny addition for future reference,

To use to array output as string (for showing in edit box for example) this snippet may work:

$result = _ArrayToString(_Stringformat($string), "")

GUICtrlSetData($Edit1, $result)
Edited by msalperen

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0