Jump to content

_StringCompact() - Compact a string to a certain number of characters left and right.


Recommended Posts

Posted (edited)

Compact a string to a certain number of characters left and right.

#include <MsgBoxConstants.au3>

MsgBox($MB_SYSTEMMODAL, '', _StringCompact(@ScriptFullPath, 15) & @CRLF)
MsgBox($MB_SYSTEMMODAL, '', _StringCompact('This will display 15 characters left and right in a string.', 15) & @CRLF)

; #FUNCTION# ====================================================================================================================
; Name ..........: _StringCompact
; Description ...: Compact a string to a certain number of characters left and right.
; Syntax ........: _StringCompact($sString[, $iChrsLeftAndRight = Default])
; Parameters ....: $sString             - A string to compact. Multi-lines aren't supported.
;                  $iChrsLeftAndRight   - [optional] An integer value. Default is 0, original string.
; Return values .: Compacted string.
; Author ........: guinness. Idea by AZJIO.
; Example .......: Yes
; ===============================================================================================================================
Func _StringCompact($sString, $iChrsLeftAndRight = Default)
    $iChrsLeftAndRight = Int($iChrsLeftAndRight)
    If StringLen($sString) > ($iChrsLeftAndRight * 2) Then
        $sString = StringRegExpReplace($sString, '(^.{' & $iChrsLeftAndRight & '}).*(.{' & $iChrsLeftAndRight & '})$', '$1...$2')
    EndIf
    Return $sString
EndFunc   ;==>_StringCompact
#include <MsgBoxConstants.au3>

MsgBox($MB_SYSTEMMODAL, '', _StringCompact(@ScriptFullPath, 15) & @CRLF)
MsgBox($MB_SYSTEMMODAL, '', _StringCompact('This will display 15 characters left and right in a string.', 15) & @CRLF)

; #FUNCTION# ====================================================================================================================
; Name ..........: _StringCompact
; Description ...: Compact a string to a certain number of characters left and right.
; Syntax ........: _StringCompact($sString[, $iChrsLeftAndRight = Default])
; Parameters ....: $sString             - A string to compact. Multi-lines are supported.
;                  $iChrsLeftAndRight   - [optional] An integer value. Default is 0, original string.
; Return values .: Compacted string.
; Author ........: guinness.
; Example .......: Yes
; ===============================================================================================================================
Func _StringCompact($sString, $iChrsLeftAndRight = Default)
    $iChrsLeftAndRight = Int($iChrsLeftAndRight)
    Local $sStringCompact = $sString
    If StringLen($sString) > ($iChrsLeftAndRight * 2) Then
        $sStringCompact = StringLeft($sString, $iChrsLeftAndRight) & '...' & StringRight($sString, $iChrsLeftAndRight)
    EndIf
    Return $sStringCompact
EndFunc   ;==>_StringCompact
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 1 month later...
Posted

Added second example using StringLeft and StringRight.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 10 months later...
Posted

Updated the regular expression of the first example.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  On 6/22/2014 at 10:30 AM, JScript said:

I liked!

But instead of _StringCompact, would not _StringCut ?

Compact refers compression and is not what happens...

JS

Good point, compact does have many connotations.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

According to Google Translator:

  Reveal hidden contents

 

JS

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

  Reveal hidden contents

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Posted

JScript,

It's based on PathCompactPathEx.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Yeah. Why do you ask?

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Sometimes you have a control label and a string (filepath) that you want to display, but it's just too long for that label. This is when such a function could be used.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)
  On 6/22/2014 at 4:52 PM, czardas said:

Okay. I have started using read only input controls to mimic labels, so you can scroll and select all the text when it doesn't all fit on a label. It's an alternative to truncation.

Great!

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  On 6/22/2014 at 5:19 PM, czardas said:

I'll post an example shortly. I can't do it right now.

Don't worry. I changed the comment after I wrote it as I realised it was wrong to ask. Sorry.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Probably something like this I would imagine...

GUICtrlCreateInput('Some text', 0, 0, 200, 25, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY)) ; EditConstants.au3

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

I think so, I also used some Edit constant to hide the border, so it looks exactly like a label - until you highlight the text. I'm not sure where I used it, I'll have to look. I saw similar controls used on the file properties GUI (on XP) and liked the solution.

Edited by czardas
Posted

This creates something like that...

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    GUICreate('', 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, $WS_EX_ACCEPTFILES)
    GUICtrlCreateInput('Some text that is super super long..................................BOO!', 0, 0, 100, 25, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY), $WS_EX_TOOLWINDOW) ; EditConstants.au3

    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

   GUIDelete()
EndFunc   ;==>Example

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...