StringExtensions.cs 1.57 KB
Newer Older
Eric Domke's avatar
Eric Domke committed
1
2
3
using System;
using System.Text;

Matt Schneeberger's avatar
Matt Schneeberger committed
4
namespace Svg.ExCSS.Model.Extensions
Eric Domke's avatar
Eric Domke committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{
    public static class StringExtensions
    {
        public static string Indent(this string value, bool friendlyForamt, int indentation)
        {
            if (!friendlyForamt)
            {
                return value;
            }

            var tabs = new StringBuilder();
            for (var i = 0; i < indentation; i++)
            {
                tabs.Append("\t");
            }

            return string.Format("{0}{1}", tabs, value);
        }

        public static string NewLineIndent(this string value, bool friendlyFormat, int indentation)
        {
            if (!friendlyFormat)
            {
                return value;
            }

            return Environment.NewLine + value.Indent(true, indentation);
        }

        public static string TrimFirstLine(this string value)
        {
            return new StringBuilder(value).TrimFirstLine().ToString();
        }

        public static StringBuilder TrimLastLine(this StringBuilder builder)
        {
            while (builder[builder.Length-1] == '\r' || builder[builder.Length-1] == '\n' || builder[builder.Length-1] == '\t')
            {
                builder.Remove(builder.Length - 1, 1);
            }

            return builder;
        }

        public static StringBuilder TrimFirstLine(this StringBuilder builder)
        {
            while (builder[0] == '\r' || builder[0] == '\n' || builder[0] == '\t')
            {
                builder.Remove(0, 1);
            }

            return builder;
        }
    }
}