1

I have a massive array of string characters that I would like to insert a carriage return every 10 characters.

For example, if my string array in Powershell is:

all_str_data = 02492340AF839e952DAB034B3D6DFCD029485DACCAEEFF...

I'd like the output to be:

all_str_data = 
02492340AF
839e952DAB
034B3D6DFC
D029485DAC
CAEEFF...

I'm eventually writing this to a file that I can use for a later step. Can someone help me here?

I'm pretty much a novice when it comes to Powershell. I haven't tried much yet and would love any additional help.

1
  • 3
    Define "massive". A few thousands? Okay, that's one option. A few billions? We might suggest something else. Commented Aug 30, 2024 at 17:08

1 Answer 1

6

One option can be to use -replace operator with regex, see https://regex101.com/r/6AoFuq/2 for details.

$all_str_data = '02492340AF839e952DAB034B3D6DFCD029485DACCAEEFF...'
$all_str_data -replace '(?<=\G.{10})(?!$)', "`r`n"

If you want a string array instead of a single multi-line string you can use the same regex but with -split:

$all_str_data -split '(?<=\G.{10})(?!$)'
Sign up to request clarification or add additional context in comments.

2 Comments

Works like a champ folks. Thanks so much for your help! The max this will be is on the order of a few million characters.
A solution without lookbehind assertion: regex101.com/r/Jw6lDP/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.