modifying strings

Welcome Forums General PowerShell Q&A modifying strings

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5438

    Points: 0
    Rank: Member

    by jgk-ps at 2013-01-15 10:12:18

    Hi guys

    Im having some issues working with strings, namely i have a list of 8 digit numbers (about 7389 of them) and i need to either remove or replace the 3rd digit and i have no idea how to go about doing this.

    for example i have a string like this: 99068003 and i need to get rid of the first zero and change it to 9968003.

    every number string i have is exactly 8 digits, however they are not in sequence, i cannot assume that the 3rd digit is always the same, or that it is always preceded by the same digit. its basically just a huge pile of seemingly random 8 digits numbers

    any takers?

    by nohandle at 2013-01-15 10:30:23

    99068003,99068003 |
    foreach {
    $asString = [string]$_

    $position = 3 -1
    $count = 1
    #remove the third character and cast it back to number

    The function is not safe in manner it does not validate its input, and hence you can get error while casting back to integer.
    Do you do math on the numbers? If no, handling them as string the whole time is probably better idea.

    There is probably mathematical solution to the problem but it requires thinking 😀

    by jgk-ps at 2013-01-15 10:44:06

    thanks, it worked exactly as i hoped it would

    by jgk-ps at 2013-01-15 10:47:29

    quick question

    how come the $position variable is 3 -1 instead of just 2?

    by nohandle at 2013-01-15 10:58:37

    To avoind magic numbers. You asked for removal of the third number, the string is indexed from zero. 3-1 is more descriptive than 2 .

The topic ‘modifying strings’ is closed to new replies.