1
00:00:00,040 --> 00:00:02,050
So let's go back into the PowerShell console,

2
00:00:02,050 --> 00:00:07,100
we'll look at how we manipulate string data by first replacing string values,

3
00:00:07,100 --> 00:00:09,310
then we'll split string values,

4
00:00:09,310 --> 00:00:12,940
and then we'll actually pad some string values as well.

5
00:00:12,940 --> 00:00:17,980
And next thing to look at is using things like replace and split so that

6
00:00:17,980 --> 00:00:21,020
we can take a string value and do something with it.

7
00:00:21,020 --> 00:00:26,440
So let's just create a new variable1,

8
00:00:26,440 --> 00:00:30,920
and what I'm going to do first is just null the values out just to make sure

9
00:00:30,920 --> 00:00:34,470
that any of the previous values that we had have been removed,

10
00:00:34,470 --> 00:00:37,290
and then what I'll do is I'll say,

11
00:00:37,290 --> 00:00:51,440
THe class instructor asked for a volunteer for a demonstration.

12
00:00:51,440 --> 00:00:53,940
Okay, that's my first variable.

13
00:00:53,940 --> 00:01:02,840
Then for my second variable, I'm going to say Jones and Tom, so two values.

14
00:01:02,840 --> 00:01:05,440
Okay, so what can we do with that?

15
00:01:05,440 --> 00:01:10,640
Well, firstly, what we can do is we can look at using replace,

16
00:01:10,640 --> 00:01:15,900
so I'm going to use my variable values that I just created,

17
00:01:15,900 --> 00:01:19,170
so variable1, and the sentence was,

18
00:01:19,170 --> 00:01:22,330
THe class instructor asked for a volunteer for a demonstration.

19
00:01:22,330 --> 00:01:25,680
But actually, what I realized is it shouldn't say instructor.

20
00:01:25,680 --> 00:01:35,240
It should say teacher, so I'm going to say "instructor", "teacher".

21
00:01:35,240 --> 00:01:36,940
So what we're doing here is I'm saying,

22
00:01:36,940 --> 00:01:40,430
get the value from variable1 then replace whatever

23
00:01:40,430 --> 00:01:43,140
you see with the word teacher.

24
00:01:43,140 --> 00:01:45,310
So I'm going to go over here, and you'll see,

25
00:01:45,310 --> 00:01:49,140
THe class teacher asked for a volunteer for a demonstration.

26
00:01:49,140 --> 00:01:52,500
Now, if I go back into variable1,

27
00:01:52,500 --> 00:01:54,980
you'll see it still says the class instructor because

28
00:01:54,980 --> 00:01:58,030
we didn't actually change the value.

29
00:01:58,030 --> 00:02:01,840
We just replaced it as it was coming back.

30
00:02:01,840 --> 00:02:02,530
Now, of course,

31
00:02:02,530 --> 00:02:07,040
that's actually a common technique that's often used that what you often

32
00:02:07,040 --> 00:02:11,190
have is a static value of some description that you may want to modify

33
00:02:11,190 --> 00:02:13,190
and change as you go up and down the script,

34
00:02:13,190 --> 00:02:16,740
but ultimately, you don't want to change the original value.

35
00:02:16,740 --> 00:02:20,540
So what we can do here is call a new variable.

36
00:02:20,540 --> 00:02:22,680
Let's say we call it replacevariable.

37
00:02:22,680 --> 00:02:25,720
I can then say variable1.

38
00:02:25,720 --> 00:02:33,090
I could repeat the same process, so replace "instructor", "teacher".

39
00:02:33,090 --> 00:02:39,550
And so now, if I do that and say replacevariable,

40
00:02:39,550 --> 00:02:46,630
that has the new value, whereas the variable1 I had still has the original value,

41
00:02:46,630 --> 00:02:50,420
but it means that I can always call back to that original one if

42
00:02:50,420 --> 00:02:53,840
I ever need to get those values to come back.

43
00:02:53,840 --> 00:02:54,210
Now,

44
00:02:54,210 --> 00:02:57,860
another option that we've got is the ability for kind of moving things around,

45
00:02:57,860 --> 00:03:01,930
so we can use regex replace to kind of move things around.

46
00:03:01,930 --> 00:03:07,860
So if we look at our variable2, for example, it says, Jones Tom.

47
00:03:07,860 --> 00:03:11,300
I actually just realized that it should be Tom Jones.

48
00:03:11,300 --> 00:03:13,570
So how do we move that around?

49
00:03:13,570 --> 00:03:18,240
Well, what we can do is we can say variable2,

50
00:03:18,240 --> 00:03:21,030
and we can use a replace, which is the standard function,

51
00:03:21,030 --> 00:03:29,300
and then now we can use some combination of regex to be able to do this.

52
00:03:29,300 --> 00:03:30,380
Now, of course, as you know,

53
00:03:30,380 --> 00:03:34,940
regex is normally a combination of certain types of brackets with

54
00:03:34,940 --> 00:03:38,040
letters to kind of identify where things are.

55
00:03:38,040 --> 00:03:40,840
So what we're doing here is we're going to take the first one,

56
00:03:40,840 --> 00:03:43,940
and we're going to split into the second one.

57
00:03:43,940 --> 00:03:49,820
So if I just do this here, a‑z, and then do a plus on this side,

58
00:03:49,820 --> 00:03:51,980
so basically, it's going to look for the space in the middle,

59
00:03:51,980 --> 00:03:53,640
take the two values.

60
00:03:53,640 --> 00:04:03,780
Once I have that split then I end up with two kind of variables, one is a $2,

61
00:04:03,780 --> 00:04:10,800
and one is $1, which basically is the output of it being chopped up.

62
00:04:10,800 --> 00:04:12,610
So you can see that we took variable to,

63
00:04:12,610 --> 00:04:15,630
which was Jones and Tom, used regex to find the first word,

64
00:04:15,630 --> 00:04:19,500
the second word, put them into various containers, so $1,

65
00:04:19,500 --> 00:04:23,700
$2, and then swap them around, so what you end up with now is Tom,

66
00:04:23,700 --> 00:04:27,230
Jones with a comma because I added the comma into the middle.

67
00:04:27,230 --> 00:04:29,780
Now, if I didn't add the comma, that could just be a space,

68
00:04:29,780 --> 00:04:31,640
and it would just say, Tom Jones.

69
00:04:31,640 --> 00:04:37,280
So we can use the replace to not actually replace words or text values,

70
00:04:37,280 --> 00:04:41,750
but actually to move things around and swap them as we need it.

71
00:04:41,750 --> 00:04:44,150
We can actually go even simpler at this point.

72
00:04:44,150 --> 00:04:48,040
So if I get rid of all of this extra piece here,

73
00:04:48,040 --> 00:04:49,950
if I just go back to replace,

74
00:04:49,950 --> 00:04:55,040
and what I can do is simply use just some quotes here,

75
00:04:55,040 --> 00:05:01,520
put the brackets, use a chevron, a‑z, and then I can do that,

76
00:05:01,520 --> 00:05:05,540
and that will literally just remove spaces.

77
00:05:05,540 --> 00:05:09,990
So the replace function serves a wider purpose than just replacing text values.

78
00:05:09,990 --> 00:05:14,340
So just be aware that it can be joined with other pieces too.

79
00:05:14,340 --> 00:05:16,840
Now, the other option that we have is to use the split.

80
00:05:16,840 --> 00:05:18,980
Now, I love using the split because often,

81
00:05:18,980 --> 00:05:21,660
when we have large amounts of information,

82
00:05:21,660 --> 00:05:24,690
we want to split it into smaller kind of pieces so

83
00:05:24,690 --> 00:05:27,390
that we can run something over it.

84
00:05:27,390 --> 00:05:30,640
So if I just type the word split, for example,

85
00:05:30,640 --> 00:05:37,850
Jan Feb Mar Apr May Jun,

86
00:05:37,850 --> 00:05:44,190
so I have a series of values that are just spaced out in that text value.

87
00:05:44,190 --> 00:05:48,570
If I do split, it'll chop them each into a specific line.

88
00:05:48,570 --> 00:05:49,880
So that's one way of doing it.

89
00:05:49,880 --> 00:05:53,040
We may get a string set value that comes back, like this.

90
00:05:53,040 --> 00:05:53,910
Now,

91
00:05:53,910 --> 00:05:57,170
I'm pretty sure that most of the data that you will get back

92
00:05:57,170 --> 00:06:00,940
from various things will be comma separated.

93
00:06:00,940 --> 00:06:04,280
So in this instance, we can change the syntax around.

94
00:06:04,280 --> 00:06:07,600
So we're going to take the value first, so this is my,

95
00:06:07,600 --> 00:06:10,240
effectively, my array of data.

96
00:06:10,240 --> 00:06:13,980
I can then say, I want to split that information,

97
00:06:13,980 --> 00:06:17,330
and then I'm going to say, I want to use a comma for that and do Enter,

98
00:06:17,330 --> 00:06:18,600
and it will do the same thing.

99
00:06:18,600 --> 00:06:22,590
So the split can either use default spaces because the

100
00:06:22,590 --> 00:06:28,640
default delimiter is just a space, or we can tell it what the delimiter might be.

101
00:06:28,640 --> 00:06:31,080
Now, we can even go even crazier than this.

102
00:06:31,080 --> 00:06:34,260
What we can do is if we bring that back here,

103
00:06:34,260 --> 00:06:38,880
I've got the split with the comma. I could then go a little bit further and say,

104
00:06:38,880 --> 00:06:40,760
only get me three values.

105
00:06:40,760 --> 00:06:42,320
Now, notice what happens.

106
00:06:42,320 --> 00:06:47,270
It splits three times and then concatenate or joins the values together,

107
00:06:47,270 --> 00:06:50,600
so I get separate Jan, separate Feb, and then a Mar,

108
00:06:50,600 --> 00:06:50,990
April, May,

109
00:06:50,990 --> 00:06:57,840
Jun all together because I asked it to give me three values out of it.

110
00:06:57,840 --> 00:06:59,280
So let's try that one again.

111
00:06:59,280 --> 00:07:03,550
But this time, what we'll do is we'll, instead of using it all on one line,

112
00:07:03,550 --> 00:07:05,330
we'll actually have our variable.

113
00:07:05,330 --> 00:07:11,640
So we've got our variable, I'm going to call it variablesplit,

114
00:07:11,640 --> 00:07:14,110
and we'll do the same thing as before, Jan,

115
00:07:14,110 --> 00:07:21,840
Feb, Mar, Apr, Maty, Jun, like so,

116
00:07:21,840 --> 00:07:26,840
so I have a variable called variablesplit, let me,

117
00:07:26,840 --> 00:07:30,040
variablesplit, and I give it my values.

118
00:07:30,040 --> 00:07:33,340
I couldn't spell May properly, but that'll do for now.

119
00:07:33,340 --> 00:07:35,940
So we have our values.

120
00:07:35,940 --> 00:07:40,680
What we can then do is because we put them into a variable,

121
00:07:40,680 --> 00:07:46,840
when I do variable and call the name, I can then use Split.

122
00:07:46,840 --> 00:07:49,310
It becomes a method of the object.

123
00:07:49,310 --> 00:07:54,330
So I can go in here and say, I want to split it on the comma and sure enough,

124
00:07:54,330 --> 00:07:55,850
I get the same again.

125
00:07:55,850 --> 00:07:59,730
So if we're using data that's been injected into a variable,

126
00:07:59,730 --> 00:08:04,040
then of course we can come and split that value as we need to.

127
00:08:04,040 --> 00:08:06,830
Now, what about if the values that you're using,

128
00:08:06,830 --> 00:08:10,160
so this is where I get to correct my Maty,

129
00:08:10,160 --> 00:08:15,740
let's say we have a combination of the separators.

130
00:08:15,740 --> 00:08:19,590
So instead of the data being nice and clean and it comes back as commas,

131
00:08:19,590 --> 00:08:22,950
maybe we have a mix of commas, and semicolons,

132
00:08:22,950 --> 00:08:23,910
and something else,

133
00:08:23,910 --> 00:08:27,660
so I'm going to make sure my variable is populated with those values,

134
00:08:27,660 --> 00:08:29,560
and I'll just make sure the values come back.

135
00:08:29,560 --> 00:08:33,640
Sure enough, you can see the mix there, commas and the semicolons.

136
00:08:33,640 --> 00:08:36,110
What I do have the ability to do, I'll tell you what,

137
00:08:36,110 --> 00:08:39,140
let me just clear this so we can see it,

138
00:08:39,140 --> 00:08:46,560
is I can go to a variablesplit, and I can use the Split method again,

139
00:08:46,560 --> 00:08:49,870
and I'm going to say, do that with a comma,

140
00:08:49,870 --> 00:08:53,760
then I'm going to say Split again and use the second

141
00:08:53,760 --> 00:08:56,900
separator that we have, and when I do Enter,

142
00:08:56,900 --> 00:08:59,740
they all split nicely.

143
00:08:59,740 --> 00:09:03,610
So we have the flexibility of being able to split the data even

144
00:09:03,610 --> 00:09:08,070
if it comes back in a strange way with multiple characters or

145
00:09:08,070 --> 00:09:11,060
delimiters that we can utilize.

146
00:09:11,060 --> 00:09:14,490
Now, of course, the last one that we can utilize here is called padding,

147
00:09:14,490 --> 00:09:19,120
so the ability for us to add values or padding of some description

148
00:09:19,120 --> 00:09:22,940
to another text value of something or a number.

149
00:09:22,940 --> 00:09:26,190
This is done normally, let's say, you're passing a number around,

150
00:09:26,190 --> 00:09:30,520
and maybe all employee numbers have a series of 0s before it,

151
00:09:30,520 --> 00:09:32,790
you could pad all of those.

152
00:09:32,790 --> 00:09:35,940
So I'm going to call my variable pad1,

153
00:09:35,940 --> 00:09:39,740
and I'm just going to write the word Demonstration.

154
00:09:39,740 --> 00:09:42,400
I am also then going to create a new one,

155
00:09:42,400 --> 00:09:47,430
pad2, and it will also have the word Demonstration,

156
00:09:47,430 --> 00:09:50,140
just text values.

157
00:09:50,140 --> 00:09:54,540
Now, what we can do here is we're going to focus on pad1.

158
00:09:54,540 --> 00:09:58,260
So pad1, what we can do is go to the variable,

159
00:09:58,260 --> 00:10:01,810
and then you'll see we have an option called PadLeft,

160
00:10:01,810 --> 00:10:06,240
which is a function or method associated to that value.

161
00:10:06,240 --> 00:10:10,740
I can then say 14, and I'll explain what 14 is,

162
00:10:10,740 --> 00:10:16,460
and then I'll just say, Added, like so.

163
00:10:16,460 --> 00:10:18,390
Now, notice what happened here.

164
00:10:18,390 --> 00:10:20,700
It says, cannot convert the paddingChar,

165
00:10:20,700 --> 00:10:25,020
and that's good because you can't add a word to the end of it.

166
00:10:25,020 --> 00:10:27,570
What you have to do is add a character.

167
00:10:27,570 --> 00:10:36,940
And what you'll see is when I did that, it added A to the beginning of that.

168
00:10:36,940 --> 00:10:42,200
If I was to repeat that, and let's change this a little bit,

169
00:10:42,200 --> 00:10:44,000
and say, PadRight,

170
00:10:44,000 --> 00:10:49,640
but we'll do that on number 2, and, let's say, we change that to

171
00:10:49,640 --> 00:10:54,940
B, you'll now get the B at the other end of it.

172
00:10:54,940 --> 00:10:57,010
So when you're padding characters,

173
00:10:57,010 --> 00:11:02,470
you need to determine what the character is that you want to kind of

174
00:11:02,470 --> 00:11:08,570
pad and then what you're trying to pad into because they are just

175
00:11:08,570 --> 00:11:14,060
effectively string methods that are in .NET itself. So it can be quite

176
00:11:14,060 --> 00:11:16,330
useful to kind of pad something, if you needed to,

177
00:11:16,330 --> 00:11:18,220
to add something to the end of it.

178
00:11:18,220 --> 00:11:21,840
But when you combine all of these together where you can replace,

179
00:11:21,840 --> 00:11:28,000
you can then split and you can pad, you're able to manipulate the string values in the best way possible.

