添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
从未表白的眼镜  ·  推荐收藏!2023 ...·  1 年前    · 
刀枪不入的乌冬面  ·  Linux ...·  1 年前    · 
喝醉的跑步机  ·  Java 8 ...·  1 年前    · 

How do test if the number of characters in $dbUserName is more than eight characters?

I have been unable to locate a command or series of commands that will let me do this. I have only been able to find if the variable is null:

if ($dbUserName) {
    Write-Output " You left Username blank"
    $dbUserName = read-host

But I would like to next test like this:

if ($dbUserName [String] > 8 ) }
    Write-Output " Please enter more than 8 characters "
    $dbUserName=read-host " Re-enter database user name"
                Odd, no one caught the logic in this code.  You're checking if the user name length is greater than 8, and upon True, you're asking them to enter more characters.  So, if the length is less than or equal to 8, it will be accepted.  I think it should be -lt or -le...
                    – Tad Adams
                Mar 13 '17 at 6:49
if ($dbUserName.length -gt 8) {
    Write-Output "Please enter more than 8 characters."
    $dbUserName = Read-Host "Re-enter database username"

Please note that you have to use -gt instead of > in your if condition. PowerShell uses the following comparison operators to compare values and test conditions:

  • -eq = equals
  • -ne = not equals
  • -lt = less than
  • -gt = greater than
  • -le = less than or equals
  • -ge = greater than or equals
  • @mjolinor Yes, that's correct. Perils of working with multiple languages. :) – Ravi Thapliyal Apr 28 '13 at 13:10 Logic of this post is still flawed. If the username has a length MORE than 8 characters, the output is: enter more than 8 characters? It should be -lt – LPChip Jun 12 '17 at 9:56 $var.length says that my string is 1 character, though it is near 30 actually. – alexkovelsky May 4 '18 at 12:32 Write-Output " Please enter more than 8 characters " $dbUserName=read-host " Re-enter database user name"

    Also if you're like me and like your curly braces to be in the same horizontal position for your code blocks, you can put that on a new line, since it's expecting a code block it will look on next line. In some commands where the first curly brace has to be in-line with your command, you can use a grave accent marker (`) to tell powershell to treat the next line as a continuation.

    Using a regular expression for such an easy task seems bound to be significantly slower. Although in such a short operation it is probably not even noticeable though. – Raúl Salinas-Monteagudo Apr 15 at 10:23 If you want to test for 8 or less the regex should be "^.{0,8}$". But I agree with @RaúlSalinas-Monteagudo that this is (slightly) slower and more needlessly complex than it needs to be for a string length check. – Twon-ha May 29 at 8:17

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

    site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.9.26.35031