📜 ⬆️ ⬇️

Elegant strings

Imagine that we need to do something with strings in .net. Something is not very difficult, but not quite simple. For example, for proper formatting, place spaces after commas in the text. What does .net offer out of the box?
Something like this:

string str = "..."; str.Replace(",", ", "); 


Wait, but we wanted to set spaces, but not replace commas! ..

Ok, let's go further.
Let's introduce censorship. We will not allow in our texts, say, the word "bear". Here so easily. We will replace each "bear" with an ellipsis.
Yeah, substitute. So it is logical to use all the same method Replace. No sooner said than done:
')
 string str = "         .    ,    ."; var result = str.Replace("", "..."); 


Hmm, dots instead of the first bear appeared, but the second was too proud and began with a capital letter. And our method before him was saved. We'll have to run a second time and change the proud "Bears" as well.

 string str = "         .    ,    ."; var result = str.Replace("", "...").Replace("", "..."); 

Fuh, it turned out. Not very nice, but it works. But does it work? Suddenly come such a "MEDVED"?
We thought, tensed and cut off such insolent ones too. But at what cost!

 string str = "         .    ,    ."; int index = str.IndexOf("", StringComparison.CurrentCultureIgnoreCase); while (index >= 0) { str = str.Remove(index, "".Length); str = str.Insert(index, "..."); index = str.IndexOf("", StringComparison.CurrentCultureIgnoreCase); } 

Something in this code is wrong. And there are two problems:
  1. Slow execution due to re-creation of lines at each step due to immunity
  2. A low-level piece of utilitarian code, which is usually referred to in a class called Util and forgotten, in the middle of a pretty semantically verified project (well, at least you can use fantasies?)

At the same time, there is a solution for improving the speed - rewrite using StringBuilder .
But what to do with quietly grumbling aesthetic feeling?

Agree, the existing interface for working with strings in .net is morally obsolete. It is archaic, not flexible enough and makes you write a lot of strange code for seemingly ordinary and simple operations time after time.
Don't forget to check for null yet. Checks index boundary values. And if you please correctly deal with the lengths of the lines.

So the idea of ​​a fluent library interface for working with strings was born.
Modern, readable, and equally well tested.

Let's see what came of it.

An example of an insert operation:
 string t = "      .    !     " .Insert(",    ").After(2, "").IgnoringCase().From(The.Beginning); t.Should().Be("      .    ,    !     "); 

It reads as Insert " " after second "" ignoring case from the beginning. Although what is it me? And so everything is clear.

Delete something:
 string t = "    ->    ->,    ->" .Remove(2, ""); transformed.Should().Be("    ->    ->,    ->"); 


And now we delete everything, taking into account the register:
 string t = "       ".RemoveAll("").IgnoringCase(); t.Should().Be("      "); 


Or even like this:
 string t = "Some very long string".RemoveChars('e', 'L', 'G').IgnoringCase(); t.Should().Be("Som vry on strin"); 


So:
 string t = "     , ".RemoveVowels().For("ru"); t.Should().Be("     , "); 


There was a place to extend the standard logic:
 bool isEmptyOrWhiteSpace = " ".IsEmpty().OrWhiteSpace(); isEmptyOrWhiteSpace.Should().Be(true); 


Pass there:
 var indexes = "    ,         " .IndexesOf("").IgnoringCase(); indexes.Should().ContainInOrder(21, 44, 64); 


And back:
 var indexes = "    ,         " .IndexesOf("").From(The.End); indexes.Should().ContainInOrder(44, 21); 


And the example with bears is compact and easy to read:
 string str = "         .    ,    ."; var result = str.ReplaceAll("").With("...").IgnoringCase(); 


The project is being actively written. Many interfaces have already been developed, and most are even implemented and tested.
But there is still a lot of work and the help of the community will be very helpful.

Quickly you can try using NuGet .
And help a project on GitHub or CodePlex .

Source: https://habr.com/ru/post/176809/


All Articles