0

In my Java to C# Conversion Project, at one point I came to use java's startsWith(string prefix, int toffset) method. I used a substring to solve it for C#.

Java Code Snippet:

String str1 = "one over the coocoo's head";
String str2 = "someone";
System.out.println(str1.startsWith(str2, 4));

C# does not have the similar method. I implemented similar thing with the following code in C#.

Console.WriteLine(str1.Substring(4).StartsWith(str2));

So basically, string.Substring(toffset).StartsWith(prefix) does the similar thing in C# as string.startsWith(prefix, toffset) in Java. Thought it might be helpful for some of you guys. Thanks.

2
Contributors
1
Reply
16
Views
4 Years
Discussion Span
Last Post by JoeProgrammer
0
// extension method, example:  if( someString.StartsWith( "blah", 3 ) ){ /* do something */ }
public static bool StartsWith ( this string s, string prefix, int toOffset )
    {
    if ( null == s ) { throw new ArgumentNullException( "this" ); }
    return s.Substring( toOffset ).StartsWith( prefix );
    }

Edited by JoeProgrammer

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.