Mercurial > repos > IBBoard
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 |
rev | line source |
---|---|
16 | 1 // This file (StringManipulation.cs) is a part of the IBBoard library and is copyright 2009 IBBoard. |
2 // | |
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. | |
4 | |
37 | 5 using System; |
6 using System.Text; | |
7 | |
8 namespace IBBoard.Lang | |
9 { | |
10 /// <summary> | |
11 /// Summary description for StringManipulation. | |
12 /// </summary> | |
13 public class StringManipulation | |
14 { | |
15 private static Encoding utf8; | |
16 | |
17 static StringManipulation() | |
18 { | |
19 utf8 = Encoding.UTF8; | |
20 } | |
21 | |
22 public static byte[] StringToBytes(string str) | |
23 { | |
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 | 43 } |
44 | |
45 public static string RemoveFromLast(string stringToTrim, char removeFrom) | |
46 { | |
47 return stringToTrim.Substring(0, stringToTrim.LastIndexOf(removeFrom)); | |
48 } | |
49 | |
50 public static string CutToLength(string stringToShorten, int length) | |
51 { | |
52 int strLength = stringToShorten.Length; | |
53 | |
54 if (length >= strLength-2) | |
55 { | |
56 return stringToShorten; | |
57 } | |
58 else | |
59 { | |
60 int diff = (strLength - length) / 2; | |
61 int halfLength = strLength / 2; | |
62 return stringToShorten.Substring(0, halfLength - diff)+"..."+stringToShorten.Substring(halfLength + diff); | |
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 | 76 } |
77 } | |
78 } |