-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringHelper.cs
97 lines (90 loc) · 3.46 KB
/
StringHelper.cs
1
2
3
4
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//-------------------------------------------------------------------------------------------
// Copyright © 2007 - 2021 Tangible Software Solutions, Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// This class is used to convert some aspects of the Java String class.
//-------------------------------------------------------------------------------------------
using System;
using System.Text;
using System.Text.RegularExpressions;
internal static class StringHelper
{
//------------------------------------------------------------------------------------
// This method is used to replace calls to the 2-arg Java String.startsWith method.
//------------------------------------------------------------------------------------
public static bool StartsWith(this string self, string prefix, int toffset)
{
return self.IndexOf(prefix, toffset, StringComparison.Ordinal) == toffset;
}
//------------------------------------------------------------------------------
// This method is used to replace most calls to the Java String.split method.
//------------------------------------------------------------------------------
public static string[] Split(this string self, string regexDelimiter, bool trimTrailingEmptyStrings)
{
string[] splitArray = Regex.Split(self, regexDelimiter);
if (trimTrailingEmptyStrings)
{
if (splitArray.Length > 1)
{
for (int i = splitArray.Length; i > 0; i--)
{
if (splitArray[i - 1].Length > 0)
{
if (i < splitArray.Length)
Array.Resize(ref splitArray, i);
break;
}
}
}
}
return splitArray;
}
//-----------------------------------------------------------------------------
// These methods are used to replace calls to some Java String constructors.
//-----------------------------------------------------------------------------
public static string NewString(byte[] bytes)
{
return NewString(bytes, 0, bytes.Length);
}
public static string NewString(byte[] bytes, int index, int count)
{
return Encoding.UTF8.GetString((byte[])(object)bytes, index, count);
}
public static string NewString(byte[] bytes, string encoding)
{
return NewString(bytes, 0, bytes.Length, encoding);
}
public static string NewString(byte[] bytes, int index, int count, string encoding)
{
return NewString(bytes, index, count, Encoding.GetEncoding(encoding));
}
public static string NewString(byte[] bytes, Encoding encoding)
{
return NewString(bytes, 0, bytes.Length, encoding);
}
public static string NewString(byte[] bytes, int index, int count, Encoding encoding)
{
return encoding.GetString((byte[])(object)bytes, index, count);
}
//--------------------------------------------------------------------------------
// These methods are used to replace calls to the Java String.getBytes methods.
//--------------------------------------------------------------------------------
public static byte[] GetBytes(this string self)
{
return GetbytesForEncoding(Encoding.UTF8, self);
}
public static byte[] GetBytes(this string self, Encoding encoding)
{
return GetbytesForEncoding(encoding, self);
}
public static byte[] GetBytes(this string self, string encoding)
{
return GetbytesForEncoding(Encoding.GetEncoding(encoding), self);
}
private static byte[] GetbytesForEncoding(Encoding encoding, string s)
{
byte[] bytes = new byte[encoding.GetByteCount(s)];
encoding.GetBytes(s, 0, s.Length, (byte[])(object)bytes, 0);
return bytes;
}
}