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

Commit 9c68453

Browse files
committed
Change GetRightPart to return null when needle doesn't exist
1 parent db2a9b6 commit 9c68453

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public static string GetRightPart(this string strVal, char needle)
358358
if (strVal == null) return null;
359359
var pos = strVal.IndexOf(needle);
360360
return pos == -1
361-
? strVal
361+
? null
362362
: strVal.Substring(pos + 1);
363363
}
364364

@@ -367,7 +367,7 @@ public static string GetRightPart(this string strVal, string needle)
367367
if (strVal == null) return null;
368368
var pos = strVal.IndexOf(needle, StringComparison.OrdinalIgnoreCase);
369369
return pos == -1
370-
? strVal
370+
? null
371371
: strVal.Substring(pos + needle.Length);
372372
}
373373

tests/ServiceStack.Text.Tests/StringExtensionsTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public void Can_GetLeftPart_and_GetLeftPart_char_needle()
2222
var str = "user:pass@w:rd";
2323
Assert.That(str.GetLeftPart(':'), Is.EqualTo("user"));
2424
Assert.That(str.GetRightPart(':'), Is.EqualTo("pass@w:rd"));
25+
26+
Assert.That(str.GetLeftPart('|'), Is.EqualTo("user:pass@w:rd"));
27+
Assert.That(str.GetRightPart('|'), Is.Null);
2528
}
2629

2730
[Test]
@@ -38,6 +41,9 @@ public void Can_GetLeftPart_and_GetLeftPart_string_needle()
3841
var str = "user::pass@w:rd";
3942
Assert.That(str.GetLeftPart("::"), Is.EqualTo("user"));
4043
Assert.That(str.GetRightPart("::"), Is.EqualTo("pass@w:rd"));
44+
45+
Assert.That(str.GetLeftPart("||"), Is.EqualTo("user::pass@w:rd"));
46+
Assert.That(str.GetRightPart("||"), Is.Null);
4147
}
4248

4349
[Test]

0 commit comments

Comments
 (0)