Get Value By Index from array - CSharp System

CSharp examples for System:Array Search

Description

Get Value By Index from array

Demo Code


using System.Text;
using System.IO;//ww  w .  j  a  v a2s .c o m
using System.Globalization;
using System.Collections.Generic;
using System;

public class Main{
        public static string GetValueByIndex(this string[] array, int index, string defaultValue)
        {
            if (array == null || array.Length == 0 || array.Length <= index) return defaultValue;

            return array[index].StringValueOrEmpty();
        }
        public static string GetValueByIndex(this string[] array, int index)
        {
            return array.GetValueByIndex(index, string.Empty);
        }
        public static string StringValueOrEmpty(this DateTime? value, string format)
        {

            if (value == null)
            {
                return string.Empty;
            }
            else
            {
                if (format != null)
                    return value.Value.ToString(format);
                else
                    return value.Value.ToString();
            }

        }
        public static string StringValueOrEmpty(this DateTime? value)
        {
            return value.StringValueOrEmpty(null);
        }
        public static string StringValueOrEmpty(this double? value)
        {

            if (value == null)
            {
                return string.Empty;
            }
            else
            {
                return value.Value.ToString();
            }

        }
        public static string StringValueOrEmpty(this object value, int maxLength)
        {
            if (value == null)
            {
                return string.Empty;
            }

            var result = value.ToString();
            if (result.Length > maxLength)
            {
                return result.Substring(0, maxLength);
            }
            else
            {
                return result;
            }
        }
        public static string StringValueOrEmpty(this object value)
        {
            if (value == null)
            {
                return string.Empty;
            }

            return value.ToString();
        }
}

Related Tutorials