简体   繁体   中英

What's the equivalent of Java substring in C#?

When I want to do a substring to find a region of text in a string in Java, I supply two indexes, one for the start , and one for the end , but in C#, I am forced to provide length of the substring as a parameter, however this becomes a problem if I don't know where I'm supposed to stop, leading me to have things like this:

verse[i].Substring(verse[i].IndexOf("start"), (verse[i].IndexOf("end") - verse[i].IndexOf("start"));

instead of just

verse[i].Substring(verse[i].IndexOf("start"), (verse[i].IndexOf("end"));

Annoyingly I have come across this issue over and over again and I wonder if I'm the only one or there's a trick I'm not aware of. How best would you go about solving this issue? (Taking cleanliness and speed into consideration)

ps: I don't like creating variables for nearly everything. Thanks

You can write your own extension method like this

var newstr = str.MySubString(start,end);

..

public static partial class MyExtensions
{
    public static string MySubString(this string s,int start,int end)
    {
        return s.Substring(start, end - start + 1);
    }
}

The answer you accepted is wrong, because you asked it wrong.

In java when you call substring(START, END) you get a substring starting at START and ending at END - 1.

in c# you just have to call like:

Substring(START, END - START);

If you add 1 as suggested, you will get a different string.

The OP asked "What's the equivalent of Java substring in C#"? I was happy to see the response since I was converting a Java method that used String.substring, into C#. However, according to the official Java docs, The substring begins at the specified beginIndex and extends to the character at index endIndex -1 This was not just a minor difference to me as I wanted to convert Java to C# with the least effort. The extension mentioned above is a great answer, and certainly I could live with that. But for my needs, a more precise Java-like version should be return s.Substring(start, end - start); not start+1

Using the Java doc examples, a test shows this is correct:

[TestMethod]
    public void TestSubString()
    {
         Assert.AreEqual("urge", "hamburger".MySubstring( 4, 8));
        Assert.AreEqual("mile", "smiles".MySubstring(1, 5));
    }

Try this extension method:

public static class StringExtensions
{
    public static string MySubstring(this string s, string start, string end)
    {
        int startI = s.IndexOf(start);
        int endI = s.IndexOf(end, startI);
        return s.Substring(startI, endI - startI + 1);
    }
}

Use it like this:

string helloWorld = "startHello Worldend".MySubstring("start", "end");
string vers = verse[i].MySubstring("start", "end");

Sometimes extension methods are saving your life. You should only be aware, that if the static class is public and in your namespace, everyone using your namespace also gets your extensions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM