字符串a =“asdf xyz 123 xx 3212”;
string seperator =“z 1”;
如果分隔符的左侧有更多字符,则需要编写一个返回0的脚本,否则返回1
在这种情况下它应该返回1
答案 0 :(得分:3)
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;
}
答案 1 :(得分:1)
我希望这符合您的要求。
/// <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;
}
答案 2 :(得分: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;
答案 3 :(得分:0)
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;
}