-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringFuncs.cs
1584 lines (1429 loc) · 113 KB
/
StringFuncs.cs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Globalization;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Text;
using System.IO;
using System.Data;
using System.Text.RegularExpressions;
[assembly: CLSCompliant(true)]
namespace MHP20ClassLib {
/// <summary>
/// Summary description for StringFunc.
/// </summary>
public class StringFuncs{
/*
*
* ومن فرعها في دبي، تقدم M: حالياً خدماتها لعملائها في المملكة العربية السعودية ومصر والعراق وأبوظبي والشارقة ورأس الخيمة. كما قدمت الشركة خدماتها خلال السنوات الأخيرة في الأراضي الفلسطينية وسوريا وعُمان. ولتعزيز ورفع مستوى عروض خدماتها الأساسية، تتمتع الشركة بعلاقات متميزة مع عدد من الشركاء المحليين في أنحاء منطقة الخليج، وبالتحديد في كل من البحرين وقطر والمملكة العربية السعودية ومصر والكويت.
* */
/// <summary>
/// Required designer variable.
/// </summary>
public StringFuncs(){
/// <summary>
/// Required for Windows.Forms Class Composition Designer support
/// </summary>
}
public static string GiveMonthName(int i) {
return GiveMonthName(i, 0);
}
public static string GiveMonthName(int i, int len){
string[] names = {"","January","February","March","April","May","June","July","August","September","October","November","December"};
string[] names2 = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
if(i < 13){
if(len == 0){
return names[i];
}else{
return names2[i];
}
}else{
return "Invalid date int";
}
}
public static bool IsValidEmail(string email){
//bool isLenientMatch = false;
//string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
//Regex reLenient = new Regex(patternLenient);
//try {
// isLenientMatch = reLenient.IsMatch(email);
//} catch (Exception ex) {
//}
return IsValidEmail(email, false);
}
public static bool IsValidEmail(string email, bool strict) {
bool isMatch = false;
string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
+ @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
+ @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
+ @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
+ @"[a-zA-Z]{2,}))$";
Regex reLenient = new Regex(patternLenient);
Regex reStrict = new Regex(patternStrict);
try {
if (strict) {
isMatch = reStrict.IsMatch(email);
} else {
isMatch = reLenient.IsMatch(email);
}
} catch (Exception ex) {
}
return isMatch;
}
public static string CleanFileAndPath(string fn) {
string input = fn + "";
input = input.Replace('\\','/');
string[] arr = input.Split('/');
return CleanFileName(arr[arr.Length-1]);
}
public static string CleanFilePath(string fn) {
string input = fn + "";
input = input.Replace('\\', '/');
string[] arr = input.Split('/');
return arr[arr.Length - 1];
}
public static string CleanFileName(string fn) {
StringBuilder sb = new StringBuilder();
char[] arr = null;
int k = 0;
if (fn.Trim().Length > 0) {
arr = fn.Trim().ToLower().ToCharArray();
for (int i = 0; i < arr.Length; i++) {
k = Convert.ToInt32(arr[i]);
// 97-122 lowercase letters 45 = "-" 46 = "." 48-57 numbers
if ((k > 96 && k < 123) || (k == 45) || (k == 46) || (k > 47 && k < 58)) {
sb.Append(Convert.ToChar(k));
}
}
}
return sb.ToString();
}
public static string RemoveIllegalUriCharacters(string s) {
StringBuilder sb = new StringBuilder();
char[] chars1 = Path.GetInvalidFileNameChars();
char[] chars2 = Path.GetInvalidPathChars();
char[] arr = null;
int k = 0;
int j = 0;
bool isokay = true;
if (s.Trim().Length > 0) {
arr = s.Trim().ToCharArray();
for (int i = 0; i < arr.Length; i++) {
isokay = true;
k = Convert.ToInt32(arr[i]);
for (int m = 0; m < chars1.Length; m++) {
j = Convert.ToInt32(chars1[m]);
if (k == j) {
isokay = false;
break;
}
}
if (isokay) {
for (int m = 0; m < chars2.Length; m++) {
j = Convert.ToInt32(chars2[m]);
if (k == j) {
isokay = false;
break;
}
}
}
if (isokay) {
sb.Append(Convert.ToChar(k));
}
}
}
return sb.ToString();
}
/// <summary>
/// Allows only Uppercase alphas, Lowercase alphas, numbers 0 - 9, and then ! # $ % & ' * + - / = ? ^ _ ` { | } ~
/// </summary>
/// <param name="fn"></param>
/// <returns></returns>
public static string CleanFormFieldEmail(string fn) {
StringBuilder sb = new StringBuilder();
char[] arr = null;
int k = 0;
arr = fn.ToCharArray();
for (int i = 0; i < arr.Length; i++) {
k = Convert.ToInt32(arr[i]);
// 65 - 90[Uppercase alphas], 97-122[Lowercase alphas], 48-57 [numbers 0 - 9], 45 [-], 46[.], 64[@], 40[(], 41[)], 39['], 44[,], 95[_], 33[!], 35[#]
if ((k > 64 && k < 91) || (k > 96 && k < 123) || (k > 47 && k < 58) || (k == 33) || (k > 34 && k < 40) || (k == 42) || (k == 43) || (k == 45) || (k == 46) || (k == 47) || (k == 61) || (k == 63) || (k == 64) || (k > 93 && k < 97) || (k > 122 && k < 127)) {
sb.Append(Convert.ToChar(k));
}
}
return sb.ToString();
}
/// <summary>
/// Allows only Uppercase alphas, Lowercase alphas, numbers 0 - 9, and then -.@()',_
/// </summary>
/// <param name="fn"></param>
/// <returns></returns>
public static string CleanFormField(string fn) {
// foreign language chars:
/*
* 192 - 214 | 216 - 246 | 248 - 304 | 308 - 448
* */
StringBuilder sb = new StringBuilder();
char[] arr = null;
int k = 0;
arr = fn.ToCharArray();
for (int i = 0; i < arr.Length; i++) {
k = Convert.ToInt32(arr[i]);
// 65 - 90[Uppercase alphas], 97-122[Lowercase alphas], 48-57 [numbers 0 - 9], 45 [-], 46[.], 64[@], 40[(], 41[)], 39['], 44[,], 95[_]
if ((k > 64 && k < 91) || (k > 96 && k < 123) || (k == 32) || (k == 39) || (k == 40) || (k == 41) || (k == 44) || (k == 45) || (k == 46) || (k > 47 && k < 58) || (k == 64) || (k == 95) || (k > 191 && k < 215) || (k > 215 && k < 247) || (k > 247 && k < 305) ||(k > 307 && k < 449)) {
sb.Append(Convert.ToChar(k));
}
}
return sb.ToString();
}
public static string CleanUserName(string fn){
char[] arr = null;
int k = 0;
fn = fn.ToLower();
arr = fn.ToCharArray();
StringBuilder sb = new StringBuilder();
for(int i=0;i<arr.Length;i++){
k = Convert.ToInt32(arr[i]);
// 97-122 lowercase letters 45 = "-" 46 = "." 48-57 numbers
// if((k > 64 && k < 91)||(k > 96 && k < 123)||(k == 45)||(k == 46) || (k > 47 && k < 58)){
// sb.Append(Convert.ToChar(k));
// }
if((k > 96 && k < 123)||(k > 47 && k < 58)){
sb.Append(Convert.ToChar(k));
}
}
return sb.ToString();
}
public static string getFolderDate() {
return DateTime.Now.ToString("yyyy-mm-dd");
}
public static string CleanNonAsciiChars(string str){
str = str.Replace("’", "'");
str = str.Replace('”','"');
str = str.Replace('“','"');
return str;
}
public static string encSql(string str,int n){
str = str.Replace("'","''");
str = str.Trim();
if(str.Length > n){
str = str.Substring(0,n);
}
return str;
}
public static string encSqlP(string str, int n) {
str = str.Trim();
if (str.Length > n) {
str = str.Substring(0, n);
}
return str;
}
public static string RemoveAllFolders(string str) {
str = str.Replace('/','\\');
int num = str.LastIndexOf('\\');
if (num > -1 && num < str.Length) {
return str.Substring(num+1);
} else {
return str;
}
}
public static string RemoveExtention(string str){
int i = 0;
i = str.LastIndexOf(".");
if(i > 0){
str = str.Substring(0,i);
}
return str;
}
public string CutLastChar(string searchstring, char searchchar){
if(searchstring != null && searchstring != ""){
if(searchstring.LastIndexOf(searchchar) == searchstring.Length-1)
searchstring = searchstring.Substring(0,searchstring.Length-1);
}
return searchstring;
}
public static string SQLinput(string str,int n){
str = str.Trim();
str = str.Replace("'","''");
if(str.Length > n){
str = str.Substring(0,n);
}
return str;
}
public static string CheckHref(string href){
if(href.IndexOf("http://") == -1 && href.IndexOf("https://") == -1 && href.IndexOf("mailto:") == -1){
href = "http://" + href;
}
return href;
}
public static string StripScripts(string strText, int n, int sql){
string tmpString ;
string le = "";
string ri = "";
int en = -1;
tmpString = strText.Trim();
tmpString = tmpString.Replace("<SCRIPT","<script");
tmpString = tmpString.Replace("</SCRIPT>","</script>");
int st = tmpString.IndexOf("<script");
if(st > -1){
while (st > -1){
en = tmpString.IndexOf("</script>",st);
le = tmpString.Substring(0,st);
ri = tmpString.Substring(en + 8);
tmpString = le + ri;
st = tmpString.IndexOf("<script");
}
}
while(tmpString.IndexOf(" ") > -1){
tmpString = tmpString.Replace(" "," ");
}
//<a href="JavaScript TODO
if(sql == 1){
tmpString = tmpString.Replace("'","''");
}
if(tmpString.Length > n){
tmpString = tmpString.Substring(0,n);
}
return tmpString;
}
public static string StripHtml(string strHtml) {
//Strips the HTML tags from strHTML
System.Text.RegularExpressions.Regex objRegExp;
string strOutput = "";
try {
objRegExp = new System.Text.RegularExpressions.Regex("<(.|\n)+?>");
// Replace all tags with a space, otherwise words either side
// of a tag might be concatenated
strOutput = objRegExp.Replace(strHtml, " ");
strOutput = strOutput.Replace("\r\n", " ");
// Replace all < and > with < and >
strOutput = strOutput.Replace("<", "<");
strOutput = strOutput.Replace(">", ">");
strOutput = strOutput.Replace("’", "'");
} catch (Exception ex) {
strOutput = strHtml;
}
return strOutput;
}
public static string StripHtmlTags(string Input, string[] AllowedTags) {
Regex StripHTMLExp = new Regex(@"(<\/?[^>]+>)");
string Output = Input;
foreach (Match Tag in StripHTMLExp.Matches(Input)) {
string HTMLTag = Tag.Value.ToLower();
bool IsAllowed = false;
foreach (string AllowedTag in AllowedTags) {
int offset = -1;
// Determine if it is an allowed tag
// "<tag>" , "<tag " and "</tag"
if (offset != 0) offset = HTMLTag.IndexOf('<' + AllowedTag + '>');
if (offset != 0) offset = HTMLTag.IndexOf('<' + AllowedTag + ' ');
if (offset != 0) offset = HTMLTag.IndexOf("</" + AllowedTag);
// If it matched any of the above the tag is allowed
if (offset == 0) {
IsAllowed = true;
break;
}
}
// Remove tags that are not allowed
if (!IsAllowed) Output = ReplaceFirst(Output, Tag.Value, "");
}
return Output;
}
public static string StripHtmlTagsAndAttributes(string Input, string[] AllowedTags) {
/* Remove all unwanted tags first */
string Output = StripHtmlTags(Input, AllowedTags);
/* Lambda functions */
//MatchEvaluator HrefMatch = m => m.Groups[1].Value + "href..;,;.." + m.Groups[2].Value;
//MatchEvaluator ClassMatch = m => m.Groups[1].Value + "class..;,;.." + m.Groups[2].Value;
//MatchEvaluator UnsafeMatch = m => m.Groups[1].Value + m.Groups[4].Value;
MatchEvaluator HrefMatch = delegate(Match m) { return m.Groups[1].Value + "href..;,;.." + m.Groups[2].Value; };
MatchEvaluator ClassMatch = delegate(Match m) { return m.Groups[1].Value + "class..;,;.." + m.Groups[2].Value; };
MatchEvaluator UnsafeMatch = delegate(Match m) { return m.Groups[1].Value + m.Groups[4].Value; };
/* Allow the "href" attribute */
Output = new Regex("(<a.*)href=(.*>)").Replace(Output,HrefMatch);
/* Allow the "class" attribute */
Output = new Regex("(<a.*)class=(.*>)").Replace(Output,ClassMatch);
/* Remove unsafe attributes in any of the remaining tags */
Output = new Regex(@"(<.*) .*=(\'|\""|\w)[\w|.|(|)]*(\'|\""|\w)(.*>)").Replace(Output,UnsafeMatch);
/* Return the allowed tags to their proper form */
Output = ReplaceAll(Output,"..;,;..", "=");
return Output;
}
public static string BlankToUnderscore(string str){
str = str.Trim();
return( str.Replace(" ","_"));
}
public static string MakeDirectoryName(string str){
string outy = "";
int k1 = 0;
char[] arr1 = str.ToCharArray();
for (int i = 0; i < arr1.Length; i++) {
k1 = Convert.ToInt32(arr1[i]);
if ((k1 > 47 && k1 < 58) || (k1 > 64 && k1 < 91) || (k1 > 96 && k1 < 123)) {
outy = outy + arr1[i];
}
}
return outy;
}
public static string Capitalise(string str){
return (str.ToUpper());
}
public static string Capitalise2(string str, bool firstonly) {
StringBuilder sb = new StringBuilder();
int count = 0;
string[] arr = str.Split(' ');
if (firstonly) {
for (int i = 0; i < arr.Length; i++) {
if (arr[i].Length > 0) {
count++;
if (count == 1) {
sb.Append(CapWord(arr[i]));
} else {
sb.Append(" " + arr[i].ToLower());
}
}
}
} else {
for (int i = 0; i < arr.Length; i++) {
if (arr[i].Length > 0) {
count++;
if (count == 1) {
sb.Append(CapWord(arr[i]));
} else {
sb.Append(" " + CapWord(arr[i]));
}
}
}
}
return (sb.ToString());
}
private static string CapWord(string s) {
string ss = s.Trim();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ss.Length; i++) {
if(i == 0){
sb.Append(ss.Substring(i, 1).ToUpper());
}else{
sb.Append(ss.Substring(i, 1).ToLower());
}
}
return (sb.ToString());
}
public static string CleanDomain(string str){
str = str.Trim();
int slash = str.LastIndexOf("\\");
if(slash==-1){
return str;
}else{
return str.Substring(slash+1);
}
}
public static string CleanFolders(string str){
str = str.Trim();
int slash = str.LastIndexOf("/");
if(slash==-1){
return str;
}else{
return str.Substring(slash+1);
}
}
public static string RandomWord(int size) {
Random random = new Random();
return RandomWord(random, size);
}
public static string RandomWord(Random random, int size) {
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++) {
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
public static string RandomWordLower(int size) {
Random random = new Random();
return RandomWordLower(random, size);
}
public static string RandomWordLower(Random random, int size) {
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++) {
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString().ToLower();
}
public static string RandomNumberString(int size){
Random random = new Random();
return RandomNumberString(random, size);
}
public static string RandomNumberString(Random random, int size) {
StringBuilder builder = new StringBuilder();
Double db = random.NextDouble();
char ch;
for (int i = 0; i < size; i++) {
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(10 * random.NextDouble() + 48)));
builder.Append(ch);
}
return builder.ToString();
}
public static int RandomNumber(int size){
Random random = new Random();
return RandomNumber(random, size);
}
public static int RandomNumber(Random random, int size) {
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++) {
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(10 * random.NextDouble() + 48)));
builder.Append(ch);
}
return Convert.ToInt32(builder.ToString());
}
public static string ConvertToKB(long str) {
string output = "";
long mod = 0;
string modout = "";
if (str > 1024000) {
mod = str % 1024000;
modout = mod.ToString();
if (modout.Length > 2) {
modout = modout.Substring(0, 2);
}
output = Convert.ToInt64(str / 1024000) + "." + modout + " mb";
} else if (str > 1024) {
output = Convert.ToInt64(str / 1024) + " kb";
} else {
output = str + " b";
}
return (output);
}
public static string ConvertToKB(int str){
string output = "";
int mod = 0;
string modout = "";
if( str > 1024000){
mod = str % 1024000;
modout = mod.ToString();
if(modout.Length > 2){
modout = modout.Substring(0,2);
}
output = Convert.ToInt32(str/1024000) + "."+ modout +" mb";
}else if( str > 1024 ){
output = Convert.ToInt32(str/1024) + " kb";
}else{
output = str + " b";
}
return( output);
}
public static string GiveTimeDateString(){
DateTime date = DateTime.Now;
return(date.ToString("dd-MM-yyyy")+" "+date.ToLongTimeString());
}
public static string GetMonth(int i){
return GiveMonthName(i, 0);
}
public static string PreparePageName(string pgname){
pgname = pgname.ToLower();
char[] letters = pgname.ToCharArray();
string final = "";
for(int i=0;i<letters.Length;i++){
if((Convert.ToInt32(letters[i]) > 96 && Convert.ToInt32(letters[i]) < 123) || (Convert.ToInt32(letters[i]) > 47 && Convert.ToInt32(letters[i]) < 58)){
final += letters[i].ToString();
}
}
return(final);
}
public static string BoolToIntString(bool chkvalue){
string outy = "";
if(chkvalue==true){
outy = "1";
}else{
outy = "0";
}
return outy;
}
public static string FixUrl(string str){
string lstr = str.ToLower();
string outy = "";
if(lstr.Substring(0,8) == "https://"){
outy = lstr;
}else if(lstr.Substring(0,7) == "http://"){
outy = lstr;
}else{
outy = "http://" + lstr;
}
return outy;
}
public static string GetFileExtention(string path){
int dot = path.LastIndexOf(".");
string ext = "";
if(dot>-1){
ext = path.ToLower().Substring((dot+1));
}
return ext;
}
public static string OnlyNumbers(string fn) {
char[] arr = null;
int k = 0;
fn = fn.ToLower();
arr = fn.ToCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.Length; i++) {
k = Convert.ToInt32(arr[i]);
// 97-122 lowercase letters 45 = "-" 46 = "." 48-57 numbers
if (k > 47 && k < 58) {
sb.Append(Convert.ToChar(k));
}
}
return sb.ToString();
}
public static bool Is0To9Only(string s) {
bool outy = true;
int k = 0;
char[] arr = s.ToCharArray();
for (int i = 0; i < arr.Length; i++) {
k = Convert.ToInt32(arr[i]);
if (k > 47 && k < 58) {
} else {
outy = false;
break;
}
}
return outy;
}
public static string OnlyMathsNumbers(string fn) {
char[] arr = null;
int k = 0;
fn = fn.ToLower();
arr = fn.ToCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.Length; i++) {
k = Convert.ToInt32(arr[i]);
// 97-122 lowercase letters 45 = "-" 46 = "." 48-57 numbers
if ((k > 41 && k < 58) || (k == 37) || (k == 60) || (k == 61) || (k == 62)) {
sb.Append(Convert.ToChar(k));
}
}
return sb.ToString();
}
public static bool CheckPassword(string pass1, string pass2, bool CaseSensitive) {
string p1 = "";
string p2 = "";
if (CaseSensitive == true) {
p1 = pass1.Trim();
p2 = pass2.Trim();
} else {
p1 = pass1.Trim().ToLower();
p2 = pass2.Trim().ToLower();
}
bool outy = false;
int k1 = 0;
int k2 = 0;
if (p1.Length == p2.Length) {
outy = true;
char[] arr1 = p1.ToCharArray();
char[] arr2 = p2.ToCharArray();
for (int i = 0; i < arr1.Length; i++) {
k1 = Convert.ToInt32(arr1[i]);
k2 = Convert.ToInt32(arr2[i]);
if (k1 != k2) {
outy = false;
}
}
}
return outy;
}
public static string CreatePassword() {
Random random = new Random();
return CreatePassword(random);
}
public static string CreatePassword(Random random) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();
string outy = "";
double n2 = Math.Floor(4 * random.NextDouble());
double n1 = 0L;
string numS = "";
string numN = "";
string numSL = "";
int tempy = 0;
string[] ULetters = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y" };
string[] LLetters = { "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y" };
string[] Numbers = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
try {
for (int i = 0; i < 10; i++) {
n1 = Math.Floor(ULetters.Length * random.NextDouble());
tempy = Convert.ToInt32(n1);
sb1.Append(ULetters[tempy]);
}
numS = sb1.ToString();
for (int i = 0; i < 10; i++) {
n1 = Math.Floor(LLetters.Length * random.NextDouble());
tempy = Convert.ToInt32(n1);
sb2.Append(LLetters[tempy]);
}
numSL = sb2.ToString();
for (int i = 0; i < 10; i++) {
n1 = Math.Floor(Numbers.Length * random.NextDouble());
tempy = Convert.ToInt32(n1);
sb3.Append(Numbers[tempy]);
}
numN = sb3.ToString();
//numS = RandomWord(random, 10).ToString();
//numN = RandomNumberString(random, 10).ToString();
//numSL = RandomWordLower(random, 10).ToString();
switch (Convert.ToInt32(n2)) {
case 0:
outy = numSL.Substring(0, 2) + numN.Substring(2, 3) + numS.Substring(5, 1) + numN.Substring(6, 2);
break;
case 1:
outy = numS.Substring(0, 3) + numN.Substring(3, 1) + numSL.Substring(4, 2) + numN.Substring(6, 2);
break;
case 2:
outy = numSL.Substring(0, 1) + numN.Substring(1, 3) + numS.Substring(4, 3) + numN.Substring(7, 1);
break;
case 3:
outy = numN.Substring(0, 2) + numS.Substring(2, 2) + numN.Substring(4, 1) + numSL.Substring(5, 3);
break;
}
} catch (Exception ex) {
outy = ex.ToString().Substring(0, 8);
}
return outy;
}
//public static string CreatePassword(Random random){
// StringBuilder sb1 = new StringBuilder();
// StringBuilder sb2 = new StringBuilder();
// StringBuilder sb3 = new StringBuilder();
// string outy = "";
// double n1 = Math.Floor(4 * random.NextDouble());
// string numS = "";
// string numN = "";
// string numSL = "";
// int tempy = 0;
// string[] ULetters = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y" };
// string[] LLetters = { "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y" };
// string[] Numbers = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
// for (int i = 0; i < 10; i++) {
// n1 = Math.Floor(ULetters.Length * random.NextDouble());
// tempy = Convert.ToInt32(n1);
// sb1.Append(ULetters[tempy]);
// }
// numS = sb1.ToString();
// for (int i = 0; i < 10; i++) {
// n1 = Math.Floor(LLetters.Length * random.NextDouble());
// tempy = Convert.ToInt32(n1);
// sb2.Append(LLetters[tempy]);
// }
// numSL = sb2.ToString();
// for (int i = 0; i < 10; i++) {
// n1 = Math.Floor(Numbers.Length * random.NextDouble());
// tempy = Convert.ToInt32(n1);
// sb3.Append(Numbers[tempy]);
// }
// numN = sb3.ToString();
// //numS = RandomWord(random, 10).ToString();
// //numN = RandomNumberString(random, 10).ToString();
// //numSL = RandomWordLower(random, 10).ToString();
// switch(Convert.ToInt32(n1)){
// case 0:
// outy = numSL.Substring(0,2) + numN.Substring(2,3) + numS.Substring(5,1) + numN.Substring(6,2);
// break;
// case 1:
// outy = numS.Substring(0,3) + numN.Substring(3,1) + numSL.Substring(4,2) + numN.Substring(6,2) ;
// break;
// case 2:
// outy = numSL.Substring(0,1) + numN.Substring(1,3) + numS.Substring(4,3) + numN.Substring(7,1) ;
// break;
// case 3:
// outy = numN.Substring(0,2) + numS.Substring(2,2) + numN.Substring(4,1) + numSL.Substring(5,3) ;
// break;
// }
// return outy;
//}
public static bool IsDate(int year, int month, int day) {
bool outy = true;
int[] leaps = {2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056, 2060, 2064, 2068, 2072, 2076, 2080, 2084, 2088, 2092, 2096};
int[] d31 = {1,3,5,7,8,10,12};
int[] d30 = {4,6,9,11};
bool leap = false;
if(month == 2){
for(int i=0;i<leaps.Length;i++){
if(year == leaps[i]){
leap = true;
}
}
if(leap == true){
if(day > 29){
outy = false;
}
}else{
if(day > 28){
outy = false;
}
}
}else{
for(int i=0;i<d30.Length;i++){
if(month == d30[i]){
if(day > 30){
outy = false;
}
}
}
}
return outy;
}
public static string base64Encode(string data) {
string encodedData = "";
try {
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
encodedData = Convert.ToBase64String(encData_byte);
} catch (Exception e) {
}
return encodedData;
}
public static string base64Decode(string data) {
string result = "";
try {
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
result = new String(decoded_char);
} catch (Exception e) {
}
return result;
}
public static string GetFilledInt(int num, int places) {
string s = num.ToString();
int slen = s.Length;
string outy = "";
if (places > slen) {
for (int i = 0; i < (places - slen); i++) {
outy += "0";
}
}
return outy + s; ;
}
public static string TextBoxToHtml(string s) {
s = s.Replace("\r\n", "<br />");
return s;
}
public static int ShowCost(int n, int rate) {
double hours = 0;
double mins = 0;
double a60 = 60;
int outy = 0;
double cost = 0;
if (n > 0 && rate > 0) {
if (n > 59) {
mins = n % a60;
hours = (n / a60);
cost = (hours * rate) + ((mins / a60) * rate);
} else {
mins = n;
cost = (mins / a60) * rate;
}
outy = Convert.ToInt32(cost);
}
return outy;
}
public static string MinutesToHours(int n) {
int hours = 0;
int mins = 0;
if (n > 59) {
mins = n % 60;
hours = Math.Abs(n / 60);
} else {
mins = n;
}
return hours.ToString() + " hours " + mins.ToString() + " minutes";
}
public static string MinutesToHours(double n) {
double hours = 0;
double mins = 0;
if (n > 59) {
mins = n % 60;
hours = Math.Floor(n / 60);
} else {
mins = n;
}
return hours.ToString() + "h " + mins.ToString() + "m";
}
public static string NumberToString(int n, int places) {
string a = n.ToString();
while (a.Length < places) {
a = "0" + a;
}
return a;
}
public static string MakeLink(string Text, string Url, bool NewWindow) {
string outy = "";
string newwin = "";
string urllower = "";
string href = "";
string firstchar = "";
int h = -1;
int i = -1;
if (NewWindow == true) newwin = " target=\"_NEW\" ";