Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

* Scripting Languages
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* aktuális (2023/24/2)
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

20180404a

string slicing

Warning! C#'s string.Substring() differs from Python's string slicing!

csharp> var s = "Fallout: New Vegas"
csharp> s.Substring(0, 4)
"Fall"                                  // Seems good, right?

csharp> s.Substring(4, 7)    
"out: Ne"                               // Ooops! What da hell?

string.Substring() has 2 parameters: start position and count. Thus, s.Substring(4, 7) means to start at position 4 and take 7 characters altogether.

Note that string.Substring() is overloaded and it also has a version with just 1 parameter. There, s.Substring(4) means to start at position 4 and take everything until the end (in Python it would be s[4:]).


To simulate Python's string slicing, I have this solution:

/// <summary>
/// Get the string slice between the two indexes.
/// Inclusive for start index, exclusive for end index.
/// </summary>
public static string Slice(this string s, int start, int end)
{
    // based on https://www.dotnetperls.com/string-slice
    if (start < 0)    // support negative indexing
    {
        start = s.Length + start;
    }
    if (end < 0)    // support negative indexing
    {
        end = s.Length + end;
    }
    if (start > s.Length)    // if the start value is too high
    {
        start = s.Length;
    }
    if (end > s.Length)    // if the end value is too high
    {
        end = s.Length;
    }
    var len = end - start;             // Calculate length
    if (len < 0)
    {
        len = 0;
    }
    return s.Substring(start, len);    // Return Substring of length
}

For usage examples, here are some tests:

[Fact]
public void Slice_string()
{
    const string s1 = "Fallout: New Vegas";
    Assert.Equal("Fall", s1.Slice(0, 4));
    Assert.Equal("out", s1.Slice(4, 7));
    Assert.Equal("out: New Vega", s1.Slice(4, 17));
    Assert.Equal("out: New Vegas", s1.Slice(4, 18));
    Assert.Equal("out: New Vegas", s1.Slice(4, 19));
    Assert.Equal("out: New Vegas", s1.Slice(4, 30));
    Assert.Equal("gas", s1.Slice(-3, s1.Length));
    Assert.Equal("Vegas", s1.Slice(-5, s1.Length));
    Assert.Equal("Vega", s1.Slice(-5, -1));
    Assert.Equal("", s1.Slice(100, 200));

    const string s2 = "batman";
    Assert.Equal("bat", s2.Slice(0, 3));
    Assert.Equal("man", s2.Slice(3, s2.Length));
    Assert.Equal("atm", s2.Slice(1, 4));
    Assert.Equal("man", s2.Slice(-3, s2.Length));
}

See the current version here.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2018 April 07, 23:52