files

Code Snippets

Title: JSON Input Attribute
Description: Set JSON Attribute
Category: C#
Visibility:  Only me
Added by: prashantkhandelwal on 5/6/2012

 Currently rated 4 by 1 user(s)

Get link for this code snippet
Collapse code snippet
Expand code snippet
Tweet this snippet
Tweet about this snippet
Delete code snippet
Edit code snippet
private Encoding GetEncoding(string contentType)
        {
            Encoding encoding = Encoding.Default;

            switch (contentType)
            {
                case "application/json; charset=UTF-7":
                    encoding= Encoding.UTF7;
                    break;
                case "application/json; charset=UTF-8":
                    encoding= Encoding.UTF8;
                    break;
                case "application/json; charset=unicode":
                    encoding= Encoding.Unicode;
                    break;
                case "application/json; charset=ascii":
                    encoding= Encoding.ASCII;
                    break;
            }

            return encoding;
        }

Title: Finding occurrences of string within string
Description: Finding occurrences of string within string
Category: C#
Visibility:  Public
Added by: Patrick Goldbach on 5/6/2012

 Currently rated 5 by 1 user(s)

Get link for this code snippet
Collapse code snippet
Expand code snippet
Tweet this snippet
Tweet about this snippet
Delete code snippet
Edit code snippet
private static IEnumerable<StringOccurance> FindOccurrences(string sourceString, string queryString)
        {
            var results = new List<StringOccurance>();

            if (string.IsNullOrEmpty(queryString))
                return results;
          
            var trimmedOffLength = 0;

            while (sourceString.IndexOf(queryString, StringComparison.CurrentCultureIgnoreCase) >= 0)
            {
                var index = sourceString.IndexOf(queryString, StringComparison.CurrentCultureIgnoreCase);
                var value = sourceString.Substring(index, queryString.Length);
                results.Add(new StringOccurance(value, trimmedOffLength + index));
                sourceString = sourceString.Remove(0, index + value.Length);
                trimmedOffLength += index + value.Length;
            }
            return results.OrderBy(x => x.Index).ToList();
        }

        public class StringOccurance
        {
            public StringOccurance(string value, int index)
            {
                Index = index;
                Value = value;
            }

            public int Length { get { return Value.Length; } }
            public int Index;
            public string Value;
        }