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#.


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.

// 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 );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.