Mercurial > repos > IBBoard
annotate Lang/StringManipulation.cs @ 37:cc7fae81afec
* Fix line terminators
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 27 Jun 2009 19:03:58 +0000 |
parents | 23fd4247fc1c |
children | a5714f82073d |
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 } | |
64 } | |
65 } | |
66 } |