annotate Lang/StringManipulation.cs @ 110:a5714f82073d

* Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way) * Make sure languages capitalise (Benoit spotted Microsoft don't capitalise their French language French)
author IBBoard <dev@ibboard.co.uk>
date Fri, 20 Jan 2012 20:50:25 +0000
parents cc7fae81afec
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
1 // This file (StringManipulation.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
2 //
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
4
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
5 using System;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
6 using System.Text;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
7
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
8 namespace IBBoard.Lang
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
9 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
10 /// <summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
11 /// Summary description for StringManipulation.
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
12 /// </summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
13 public class StringManipulation
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
14 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
15 private static Encoding utf8;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
16
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
17 static StringManipulation()
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
18 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
19 utf8 = Encoding.UTF8;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
20 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
21
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
22 public static byte[] StringToBytes(string str)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
23 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
24 return utf8.GetBytes(str);
30
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
25 }
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
26
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
27 public static string ByteArrayToHexString(byte[] arr)
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
28 {
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
29 return ByteArrayToHexString(arr, true);
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
30 }
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
31
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
32 public static string ByteArrayToHexString(byte[] arr, bool spaceBytes)
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
33 {
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
34 StringBuilder sb = new StringBuilder(arr.Length);
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
35 string format = (spaceBytes ? "{0,-3:x2}" : "{0:x2}");
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
36
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
37 foreach (byte b in arr)
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
38 {
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
39 sb.Append(String.Format(format, b));
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
40 }
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
41
23fd4247fc1c * Add "big-endian" binary reader that reads ints/shorts in big-endian format
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
42 return sb.ToString().TrimEnd();
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
43 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
44
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
45 public static string RemoveFromLast(string stringToTrim, char removeFrom)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
46 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
47 return stringToTrim.Substring(0, stringToTrim.LastIndexOf(removeFrom));
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
48 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
49
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
50 public static string CutToLength(string stringToShorten, int length)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
51 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
52 int strLength = stringToShorten.Length;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
53
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
54 if (length >= strLength-2)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
55 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
56 return stringToShorten;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
57 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
58 else
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
59 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
60 int diff = (strLength - length) / 2;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
61 int halfLength = strLength / 2;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
62 return stringToShorten.Substring(0, halfLength - diff)+"..."+stringToShorten.Substring(halfLength + diff);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
63 }
110
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
64 }
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
65
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
66 public static string UpperFirst(string toUpper)
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
67 {
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
68 if (string.IsNullOrEmpty(toUpper))
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
69 {
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
70 return string.Empty;
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
71 }
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
72
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
73 char[] chars = toUpper.ToCharArray();
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
74 chars[0] = char.ToUpper(chars[0]);
a5714f82073d * Add method for upper-casing just first character (based on DotNetPerls assertion that it is quicker this way)
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
75 return new string(chars);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
76 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
77 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 30
diff changeset
78 }