Gets the char in a string at a given position, but counting from right to left - CSharp System

CSharp examples for System:Char

Description

Gets the char in a string at a given position, but counting from right to left

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using System;/*from ww w.  j  a  va2  s .  co  m*/

public class Main{
        //Gets the char in a string at a given position, but counting from right to left
        //string, 0 -> g
        public static char CharRight(string input, int index)
        {
            if (input.Length - index - 1 >= input.Length ||
                input.Length - index - 1 < 0)
                return new char();

            string str = input.Substring(input.Length - index - 1, 1);
            return str[0];
        }
}

Related Tutorials