annotate Lang/StringManipulation.cs @ 16:0352fa33ee8f

Closes #8 - license code * Add LGPL header to IBBoard utility library * Add COPYING.LGPL and COPYING.GPL with details of license
author IBBoard <dev@ibboard.co.uk>
date Sun, 25 Jan 2009 15:04:31 +0000
parents 961030992bd2
children 23fd4247fc1c
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
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);
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 public static string RemoveFromLast(string stringToTrim, char removeFrom)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 return stringToTrim.Substring(0, stringToTrim.LastIndexOf(removeFrom));
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 public static string CutToLength(string stringToShorten, int length)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 int strLength = stringToShorten.Length;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 if (length >= strLength-2)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 return stringToShorten;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 else
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 int diff = (strLength - length) / 2;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 int halfLength = strLength / 2;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 return stringToShorten.Substring(0, halfLength - diff)+"..."+stringToShorten.Substring(halfLength + diff);
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 }
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 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 }