Ejemplo n.º 1
0
        // You should consider a SectionIndex type for findsection, findtag and findbrackets.

        //public static string CutToSection(this string html,

        //public static StringIndex FindSection(this string html, string start, string end, bool CutSearch)
        //{
        //    throw new NotImplementedException();
        //}

        /// <summary>
        /// Gets the first instance of a tag (such as &lt;div blah blah &gt;&lt;/div&gt;) in a string.
        /// The tag can contain other tags.
        /// </summary>
        /// <param name="html">The string in which the tag should be located.</param>
        /// <param name="tag">The name of the tag to locate (eg. "div" for &lt;div&gt;&lt;/div&gt;)</param>
        /// <param name="removeTag">true, if tag start/end should be removed from the return value; otherwise, false.</param>
        /// <returns>A <see cref="String"/> containing the tag and its contents.</returns>
        public static string CutToTag(this string html, string tag, bool removeTag)
        {
            StringIndex index = FindTag(html, tag, removeTag);

            if (index == StringIndex.Empty)
            {
                throw new ArgumentOutOfRangeException("The provided tag could not be found.");
            }
            else
            {
                return(html.Substring(index.First, index.Length));
            }
        }
Ejemplo n.º 2
0
        private static string cutToBrackets(this string html, char first, char last, bool CutBrackets)
        {
            StringIndex index = findBrackets(html, first, last, !CutBrackets);

            if (index.IsEmpty)
            {
                return(string.Empty);
            }
            else
            {
                return(html.Substring(index.First, index.Length));
            }
        }
Ejemplo n.º 3
0
        public StringParser CutToBrackets(char first, char last, bool CutBrackets)
        {
            StringIndex index = FindBrackets(first, last, !CutBrackets);

            if (index.IsEmpty)
            {
                return(new StringParser(string.Empty));
            }
            else
            {
                return(new StringParser(this.html.Substring(index.First, index.Length)));
            }

            /*
             * int start = this.html.IndexOf(first);
             * int offset = start + 1;
             * int tagsOpen = 1;
             *
             * int fStart = -1, fEnd = -1;
             *
             * if (start == -1)
             *  return new StringParser(string.Empty);
             *
             * while (tagsOpen > 0)
             * {
             *  fStart = this.html.IndexOf(first, offset);
             *  fEnd = this.html.IndexOf(last, offset);
             *
             *  if (fStart == -1 && fEnd == -1)
             *      return new StringParser(string.Empty);
             *
             *  if (smallestIndex(fStart, fEnd) == fStart)
             *  {
             *      offset = fStart + 1;
             *      tagsOpen++;
             *  }
             *  else
             *  {
             *      offset = fEnd + 1;
             *      tagsOpen--;
             *  }
             * }
             * if (CutBrackets)
             *  return new StringParser(html.Substring(start + 1, fEnd - 1));
             * else
             *  return new StringParser(html.Substring(start, fEnd - start + 1));*/
        }