-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpropercase_strings.PS1
More file actions
66 lines (49 loc) · 1.74 KB
/
propercase_strings.PS1
File metadata and controls
66 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# $Id: propercase_strings.PS1,v 1.1.1.1 2008/08/28 00:58:34 powem Exp $
# Purpose:
# $Date: 2008/08/28 00:58:34 $
# convert a string into proper case (e.g., from THIS to This)
#String.prototype.toProperCase = function()
#{
# return this.toLowerCase().replace(/^(.)|\s(.)/g,
# function($1) { return $1.toUpperCase(); });
#}
$strTest = "12345, `"this one, too`", `"that one, also`""
function getStream([string]$filePath){
$sr = New-Object system.IO.StreamReader($filePath);
return $sr;
}
function getSentence([string]$entry){
$re = New-Object system.Text.RegularExpressions.Regex('([0-9]+),\s?"(.*)",\s?"(.*)"');
$temp = [regex]::Match($entry,$re);
$c = 0;
$itemsHash = @{};
foreach ($item in $temp.Groups){
$itemsHash[$c++] = ($item.Value).TrimStart();
}
return $itemsHash;
}
function toProperCase([string]$newString){
# return this.toLowerCase().replace(/^(.)|\s(.)/g,
# function($1) { return $1.toUpperCase(); });
$temp = ($newString.toLower()).Replace('^(.)|\s(.)', {return $1.toUpper();});
$letter = ($newString.toCharArray())[0];
$letter = ($letter.toString()).ToUpper();
$newString = $newString.toLower();
return $temp;
#return $newString -replace '^.',$letter;
}
function properCaseString([string]$sentence){
$tempre = New-Object system.Text.RegularExpressions.Regex('\sin\s|\sa\s|\sthe\s');
$tempSentenceHash = getSentence($sentence);
$tempString = [regex]::Split($tempSentenceHash[2],",");
for ($i=0; $i -lt $tempString.Length; $i++){
if ($i -gt 0 -and $tempString[$i] -notmatch $tempre){
$tempString[$i] = ($tempString[$i]).TrimStart();
$tempString[$i] = toProperCase($tempString[$i]);
} else {
$tempString[$i] = toProperCase($tempString[$i]);
}
}
return $tempString;
}
toProperCase("This is the one");