which side has more characters
string a = "asdf xyz 123 xx 3212";
string seperator = "z 1";
need to write a script that returns 0 if left of the seperator has more characters, otherwise return 1
in this case it should retu开发者_StackOverflow中文版rn 1
quick and dirty:
private int YourMethod(string a, string separator)
{
if (a.IndexOf(seperator) > 0)
{
if ((a.Length - seperator.Length) / 2 > a.IndexOf(seperator))
return 1;
else
return 0;
}
return 0;
}
I hope this satisfies your requirements.
/// <summary>
/// Determines whether a string has more characters to the left of the separator.
/// </summary>
/// <param name="a">An arbitrary string, possibly delimited into two parts.</param>
/// <param name="separator">The characters that partition the string.</param>
/// <returns>0 if left of the separator has more characters, otherwise returns 1.</returns>
/// <exception cref="ArgumentException">No separator was supplied.</exception>
public static int MoreCharactersLeftOfSeparator(string a, string separator)
{
if (string.IsNullOrEmpty(separator))
throw new ArgumentException("No separator was supplied.", "separator");
if (a == null)
return 1;
int separatorIndex = a.LastIndexOf(separator, StringComparison.Ordinal);
if (separatorIndex == -1)
return 1;
int charactersRight = a.Length - separatorIndex - separator.Length;
if (charactersRight >= separatorIndex)
return 1;
return 0;
}
int left = a.IndexOf(separator);
if (left < 0)
return -1; //no separator?
int right = a.Length - (left + 1 + separator.Length);
if (left > right)
return 0;
else
return 1;
public static LeftOrRightString(string s, string separator)
{
var strArr = s.Split(separator);
if (strArr.Count != 2)
throw new Exception();
return strArr[0].Length < strArr[1].Length;
}
精彩评论