locked
Count space " " RRS feed

  • Question

  • Sir's
    
    Pls, i would find all free space in this txt-line?
    and after creat en second ligne.
    
    How i have to do.
    
    My beste Regards
    
    Arnold
    
    
    ==================
    
    cls
    
    $test = "eutururu: jfot juyyt rtre ltl djfu; 12 345 dkdoe siiy XXhwtt. 33 kiyXy vhgd clr!" 
    $test.length
    $ins = "`n"
    ""
    if ($test.lenght -lt 35)
    {   
        $pos = [int]$test.length / 2
        $num
        $test.Insert($pos,$ins)
    }
    
    
    

    Thursday, March 13, 2014 3:50 PM

Answers

  • Count spaces in a string:


    $test = "eutururu: jfot juyyt rtre ltl djfu; 12 345 dkdoe siiy XXhwtt. 33 kiyXy vhgd clr!"
    
    $numSpaces = 0
    $test.ToCharArray() | foreach-object {
      if ( $_ -eq ' ' ) { $numSpaces++ }
    }
    
    "The string contains $numSpaces spaces"
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Thursday, March 13, 2014 6:00 PM
    • Marked as answer by Bill_Stewart Monday, May 19, 2014 10:02 PM
    • Unmarked as answer by arnold_ Sunday, August 7, 2016 7:56 PM
    • Marked as answer by Bill_Stewart Friday, August 12, 2016 4:42 PM
    Thursday, March 13, 2014 4:40 PM

All replies

  • Count spaces in a string:


    $test = "eutururu: jfot juyyt rtre ltl djfu; 12 345 dkdoe siiy XXhwtt. 33 kiyXy vhgd clr!"
    
    $numSpaces = 0
    $test.ToCharArray() | foreach-object {
      if ( $_ -eq ' ' ) { $numSpaces++ }
    }
    
    "The string contains $numSpaces spaces"
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Thursday, March 13, 2014 6:00 PM
    • Marked as answer by Bill_Stewart Monday, May 19, 2014 10:02 PM
    • Unmarked as answer by arnold_ Sunday, August 7, 2016 7:56 PM
    • Marked as answer by Bill_Stewart Friday, August 12, 2016 4:42 PM
    Thursday, March 13, 2014 4:40 PM
  • Alternate method:

    $c=($test|Measure-Object -IgnoreWhiteSpace -Character).Characters
    $x=($test|Measure-Object -Character).Characters
    $x-$c


    ¯\_(ツ)_/¯

    • Proposed as answer by Mike Laughlin Thursday, March 13, 2014 6:00 PM
    Thursday, March 13, 2014 5:07 PM
  • Hi, @B and @J Many thanks for your remply! I m sorry, Now I hangs or I m blind... $numSpaces/2 = 7 also: $x-$c/2 = 7 For this case: But now i have to find the 7th space? I have to insert $ins = "`n" at the [int]7th space. To have 2 lignes .... My beste Regards Arnold
    Thursday, March 13, 2014 6:03 PM
  • What?  The seventh space is just number 7.

    What is it you are trying to do?  You asked hwo to count spaces. Apparently that is not what you really wanted.


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 6:13 PM
  • Tell what you need to do, not how you think it should be done.

    Beware of the XY problem.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 6:17 PM
  • Hi Sir,

    Many thanks thanks for your reply!

    Yes, i needet!

    Please, i have to insert "$ins = "`n" " at the 7th space.

    that's allways wath i would doing

    My beste regards

    Arnold

    Thursday, March 13, 2014 6:34 PM
  • Please, i have to insert "$ins = "`n" " at the 7th space.

    Why?


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 6:35 PM
  • $words=$test.Split(' ')

    $x=$words[0..6]
    $x+="`n"
    $x+=$words{7..99]
    "$x"


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 6:39 PM
  • Hi Sir,

    @Bill, I would cat large ligne over 8 "if" 80 characters per ligne

    by to use "if or %"

    The last 2 stuff works fin.

    but now i see with split is better to do.

    See the answere from "JRV"

    Do you have an ander solution?

    @ JRV, many thanks for the last stuff.

    My beste Regards

    Arnold

    Thursday, March 13, 2014 7:12 PM
  • You still have not explained exactly why you need to do it in the first place.

    You might be asking the wrong question.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 7:20 PM
  • Hi @Bill,
    
    Any is possible.
    
    Your stuff togetter with the stuff from JRV works fin!!
    
    =======================
    
    
    cls
    
    $test = "eutururu: jfot juyyt rtre ltl djfu; 12 345 dkdoe siiy XXhwtt. 33 kiyXy vhgd clr!" 
    
    $numSpaces = 0
    $test.ToCharArray() | foreach-object {
      if ( $_ -eq ' ' ) { $numSpaces++ }
    }
    
    [int]$StartSpace = $numSpaces/2
    
    $words=$test.Split(' ')
    $x=$words[0..$space]
    $x+="`n"
    $x+=$words[([int]($StartSpace+1))..$numSpaces]
    "$x"
    
    only at the 2th ligne i get 1 space to match on the begining...
    
    eutururu: jfot juyyt rtre ltl djfu; 12 345 
     dkdoe siiy XXhwtt. 33 kiyXy vhgd clr!
    
    ???
    
    My beste regards
    
    Arnold
    

     

    Thursday, March 13, 2014 8:16 PM
  • Sorry, but I do not understand your question or what you're trying to do.

    There may be a language barrier. If that is the case, try asking your question in a forum that's in your native language, or ask someone to help you translate into clearer English.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 8:23 PM