Mercurial > repos > IBBoard
annotate Lang/StringManipulation.cs @ 30:23fd4247fc1c
* Add "big-endian" binary reader that reads ints/shorts in big-endian format
* Add methods to String Manipulation to output a byte string as hex characters
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 19 Apr 2009 11:31:47 +0000 |
parents | 0352fa33ee8f |
children | cc7fae81afec |
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 | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 using System; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using System.Text; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 namespace IBBoard.Lang |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 /// <summary> |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 /// Summary description for StringManipulation. |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 /// </summary> |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 public class StringManipulation |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 private static Encoding utf8; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 static StringManipulation() |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 utf8 = Encoding.UTF8; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 public static byte[] StringToBytes(string str) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
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(); |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 public static string RemoveFromLast(string stringToTrim, char removeFrom) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 return stringToTrim.Substring(0, stringToTrim.LastIndexOf(removeFrom)); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 public static string CutToLength(string stringToShorten, int length) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 int strLength = stringToShorten.Length; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 if (length >= strLength-2) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 return stringToShorten; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 else |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 int diff = (strLength - length) / 2; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 int halfLength = strLength / 2; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 return stringToShorten.Substring(0, halfLength - diff)+"..."+stringToShorten.Substring(halfLength + diff); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 } |