views:

120

answers:

4

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 return 1

+3  A: 

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;
}
roman m
A: 
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;
o.k.w
+1  A: 

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;
    }
Jeffrey L Whitledge
+1 for ///summary :D
roman m
A: 
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;
}
Sani Huttunen