This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringTools.cs
More file actions
80 lines (69 loc) · 2.17 KB
/
StringTools.cs
File metadata and controls
80 lines (69 loc) · 2.17 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace eBayKleinanzeigenTracker
{
class StringTools
{
public static bool StringIsInt(string str)
{
try {
Convert.ToInt32(str);
return true;
} catch {
return false;
}
}
public static bool StringIsLong(string str)
{
try {
Convert.ToInt64(str);
return true;
} catch {
return false;
}
}
public static string ExtractIntFromString(string str)
{
StringBuilder sb = new StringBuilder();
foreach(char c in str.ToCharArray())
{
if (Char.IsDigit(c)) sb.Append(c);
}
if (sb.Length == 0) return "";
int n = Int32.MaxValue;
try
{
n = Int32.Parse(sb.ToString());
} catch{}
return Math.Min(99999999, n).ToString();
}
public static string[] ExtractGroups(string str, string start, string end)
{
List<string> groups = new List<string>();
while(str.Contains(start) && str.Contains(end))
{
int startIndex = str.IndexOf(start) + start.Length;
int endIndex = str.IndexOf(end, startIndex);
if (endIndex == -1) break;
string groupStr = "";
if (endIndex > startIndex) groupStr = str.Substring(startIndex, endIndex - startIndex);
groups.Add(groupStr);
str = ReplaceFirst(str, start, "");
str = ReplaceFirst(str, end, "");
}
return groups.ToArray();
}
public static string ReplaceFirst(string text, string search, string replace)
{
int pos = text.IndexOf(search);
if (pos < 0)
{
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}
}
}