locked
max length of powerhshell string? RRS feed

  • Question

  • In the docs it says that a string in powershell is a fixed length but it doesn't say how long it is.

    I need to now that to be able to determine if I Can use a string in my code

    • Moved by Bill_Stewart Tuesday, March 25, 2014 3:17 PM Abandoned
    Monday, November 18, 2013 8:37 AM

Answers

  • Hi,

    as jrv pointed out Strings in PowerShell (and most of the other programming languages) are of "Fixed length" or immutable (they cannot change). .net also offers a mutable string object System.Text.StringBuilder, which should be your preference if you change the string a lot through out its lifetime, since it does not re-create the object when you add to it:

    $s = New-Object Text.StringBuilder "test"
    [void]$s.Append(" more")
    $s.ToString()


    The theoretical  max length of a String is 2147483647, but you are more likely to run out of memory before you ever reach that limit.


    • Edited by Dirk_74 Monday, November 18, 2013 9:55 AM
    • Proposed as answer by Dirk_74 Thursday, November 28, 2013 8:25 AM
    • Marked as answer by mats42 Wednesday, April 8, 2015 2:09 PM
    Monday, November 18, 2013 9:54 AM
  • This will show the maximum length

    $s = New-Object Text.StringBuilder "test"
    $s.MaxCapacity

    PS C:\Windows\system32> $s = New-Object Text.StringBuilder "test"
    $s.MaxCapacity
    2147483647


    Regards Chen V [MCTS SharePoint 2010]

    • Marked as answer by mats42 Wednesday, April 8, 2015 2:09 PM
    Monday, November 18, 2013 10:18 AM
  • dotNet defines a string as this in th constructor:

    String(char c,Int32 length)

    dotNet:

    [Int32]::Maxvalue
    2147483647

    t=That is: (2^31) - 1

    The string needs to fit in memory.  Do you have 2.1 Gb to spare?


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Thursday, November 28, 2013 8:43 AM
    • Marked as answer by mats42 Wednesday, April 8, 2015 2:09 PM
    Monday, November 18, 2013 11:03 AM

All replies

  • $s='hello'
    $s.length

    That is its length.

    A string is recreated if you add to it:

    $s+='World'
    $s.Length

    Make a big string"

    PS > $s='X' * 1Mb
    PS  $s.Length
    1048576


    ¯\_(ツ)_/¯

    Monday, November 18, 2013 9:03 AM
  • Hi,

    as jrv pointed out Strings in PowerShell (and most of the other programming languages) are of "Fixed length" or immutable (they cannot change). .net also offers a mutable string object System.Text.StringBuilder, which should be your preference if you change the string a lot through out its lifetime, since it does not re-create the object when you add to it:

    $s = New-Object Text.StringBuilder "test"
    [void]$s.Append(" more")
    $s.ToString()


    The theoretical  max length of a String is 2147483647, but you are more likely to run out of memory before you ever reach that limit.


    • Edited by Dirk_74 Monday, November 18, 2013 9:55 AM
    • Proposed as answer by Dirk_74 Thursday, November 28, 2013 8:25 AM
    • Marked as answer by mats42 Wednesday, April 8, 2015 2:09 PM
    Monday, November 18, 2013 9:54 AM
  • This will show the maximum length

    $s = New-Object Text.StringBuilder "test"
    $s.MaxCapacity

    PS C:\Windows\system32> $s = New-Object Text.StringBuilder "test"
    $s.MaxCapacity
    2147483647


    Regards Chen V [MCTS SharePoint 2010]

    • Marked as answer by mats42 Wednesday, April 8, 2015 2:09 PM
    Monday, November 18, 2013 10:18 AM
  • dotNet defines a string as this in th constructor:

    String(char c,Int32 length)

    dotNet:

    [Int32]::Maxvalue
    2147483647

    t=That is: (2^31) - 1

    The string needs to fit in memory.  Do you have 2.1 Gb to spare?


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Thursday, November 28, 2013 8:43 AM
    • Marked as answer by mats42 Wednesday, April 8, 2015 2:09 PM
    Monday, November 18, 2013 11:03 AM
  • Suggestion.  If you need to manipulate very large strings consider using file IO.  Stream is good but a memeory mapped file Is more efficient.


    ¯\_(ツ)_/¯

    Monday, November 18, 2013 11:07 AM