Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit e7c833c

Browse files
committed
Add LastLeftPart/LastRightPart and rename GetLeft/RightPart
1 parent 9c68453 commit e7c833c

File tree

2 files changed

+72
-14
lines changed

2 files changed

+72
-14
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private static byte[] FastToUtf8Bytes(string strVal)
335335
return bytes;
336336
}
337337

338-
public static string GetLeftPart(this string strVal, char needle)
338+
public static string LeftPart(this string strVal, char needle)
339339
{
340340
if (strVal == null) return null;
341341
var pos = strVal.IndexOf(needle);
@@ -344,7 +344,7 @@ public static string GetLeftPart(this string strVal, char needle)
344344
: strVal.Substring(0, pos);
345345
}
346346

347-
public static string GetLeftPart(this string strVal, string needle)
347+
public static string LeftPart(this string strVal, string needle)
348348
{
349349
if (strVal == null) return null;
350350
var pos = strVal.IndexOf(needle, StringComparison.OrdinalIgnoreCase);
@@ -353,7 +353,7 @@ public static string GetLeftPart(this string strVal, string needle)
353353
: strVal.Substring(0, pos);
354354
}
355355

356-
public static string GetRightPart(this string strVal, char needle)
356+
public static string RightPart(this string strVal, char needle)
357357
{
358358
if (strVal == null) return null;
359359
var pos = strVal.IndexOf(needle);
@@ -362,7 +362,7 @@ public static string GetRightPart(this string strVal, char needle)
362362
: strVal.Substring(pos + 1);
363363
}
364364

365-
public static string GetRightPart(this string strVal, string needle)
365+
public static string RightPart(this string strVal, string needle)
366366
{
367367
if (strVal == null) return null;
368368
var pos = strVal.IndexOf(needle, StringComparison.OrdinalIgnoreCase);
@@ -371,6 +371,42 @@ public static string GetRightPart(this string strVal, string needle)
371371
: strVal.Substring(pos + needle.Length);
372372
}
373373

374+
public static string LastLeftPart(this string strVal, char needle)
375+
{
376+
if (strVal == null) return null;
377+
var pos = strVal.LastIndexOf(needle);
378+
return pos == -1
379+
? strVal
380+
: strVal.Substring(0, pos);
381+
}
382+
383+
public static string LastLeftPart(this string strVal, string needle)
384+
{
385+
if (strVal == null) return null;
386+
var pos = strVal.LastIndexOf(needle, StringComparison.OrdinalIgnoreCase);
387+
return pos == -1
388+
? strVal
389+
: strVal.Substring(0, pos);
390+
}
391+
392+
public static string LastRightPart(this string strVal, char needle)
393+
{
394+
if (strVal == null) return null;
395+
var pos = strVal.LastIndexOf(needle);
396+
return pos == -1
397+
? null
398+
: strVal.Substring(pos + 1);
399+
}
400+
401+
public static string LastRightPart(this string strVal, string needle)
402+
{
403+
if (strVal == null) return null;
404+
var pos = strVal.LastIndexOf(needle, StringComparison.OrdinalIgnoreCase);
405+
return pos == -1
406+
? null
407+
: strVal.Substring(pos + needle.Length);
408+
}
409+
374410
public static string[] SplitOnFirst(this string strVal, char needle)
375411
{
376412
if (strVal == null) return TypeConstants.EmptyStringArray;

tests/ServiceStack.Text.Tests/StringExtensionsTests.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public void Can_SplitOnFirst_char_needle()
1717
}
1818

1919
[Test]
20-
public void Can_GetLeftPart_and_GetLeftPart_char_needle()
20+
public void Can_LeftPart_and_LeftPart_char_needle()
2121
{
2222
var str = "user:pass@w:rd";
23-
Assert.That(str.GetLeftPart(':'), Is.EqualTo("user"));
24-
Assert.That(str.GetRightPart(':'), Is.EqualTo("pass@w:rd"));
23+
Assert.That(str.LeftPart(':'), Is.EqualTo("user"));
24+
Assert.That(str.RightPart(':'), Is.EqualTo("pass@w:rd"));
2525

26-
Assert.That(str.GetLeftPart('|'), Is.EqualTo("user:pass@w:rd"));
27-
Assert.That(str.GetRightPart('|'), Is.Null);
26+
Assert.That(str.LeftPart('|'), Is.EqualTo("user:pass@w:rd"));
27+
Assert.That(str.RightPart('|'), Is.Null);
2828
}
2929

3030
[Test]
@@ -36,14 +36,14 @@ public void Can_SplitOnFirst_string_needle()
3636
}
3737

3838
[Test]
39-
public void Can_GetLeftPart_and_GetLeftPart_string_needle()
39+
public void Can_LeftPart_and_RightPart_string_needle()
4040
{
4141
var str = "user::pass@w:rd";
42-
Assert.That(str.GetLeftPart("::"), Is.EqualTo("user"));
43-
Assert.That(str.GetRightPart("::"), Is.EqualTo("pass@w:rd"));
42+
Assert.That(str.LeftPart("::"), Is.EqualTo("user"));
43+
Assert.That(str.RightPart("::"), Is.EqualTo("pass@w:rd"));
4444

45-
Assert.That(str.GetLeftPart("||"), Is.EqualTo("user::pass@w:rd"));
46-
Assert.That(str.GetRightPart("||"), Is.Null);
45+
Assert.That(str.LeftPart("||"), Is.EqualTo("user::pass@w:rd"));
46+
Assert.That(str.RightPart("||"), Is.Null);
4747
}
4848

4949
[Test]
@@ -54,6 +54,17 @@ public void Can_SplitOnLast_char_needle()
5454
Assert.That(parts[1], Is.EqualTo("pass@word"));
5555
}
5656

57+
[Test]
58+
public void Can_LastLeftPart_and_LastRightPart_char_needle()
59+
{
60+
var str = "user:name:pass@word";
61+
Assert.That(str.LastLeftPart(':'), Is.EqualTo("user:name"));
62+
Assert.That(str.LastRightPart(':'), Is.EqualTo("pass@word"));
63+
64+
Assert.That(str.LastLeftPart('|'), Is.EqualTo("user:name:pass@word"));
65+
Assert.That(str.LastRightPart('|'), Is.Null);
66+
}
67+
5768
[Test]
5869
public void Can_SplitOnLast_string_needle()
5970
{
@@ -62,6 +73,17 @@ public void Can_SplitOnLast_string_needle()
6273
Assert.That(parts[1], Is.EqualTo("pass@word"));
6374
}
6475

76+
[Test]
77+
public void Can_LastLeftPart_and_LastRightPart_string_needle()
78+
{
79+
var str = "user::name::pass@word";
80+
Assert.That(str.LastLeftPart("::"), Is.EqualTo("user::name"));
81+
Assert.That(str.LastRightPart("::"), Is.EqualTo("pass@word"));
82+
83+
Assert.That(str.LastLeftPart("||"), Is.EqualTo("user::name::pass@word"));
84+
Assert.That(str.LastRightPart("||"), Is.Null);
85+
}
86+
6587
private static readonly char DirSep = Path.DirectorySeparatorChar;
6688
private static readonly char AltDirSep = Path.DirectorySeparatorChar == '/' ? '\\' : '/';
6789

0 commit comments

Comments
 (0)