C# .NET - largest third digit from a number

Asked By kshama parashar
04-Feb-12 12:54 AM
hello to all
i have a number and i want to find 3rd digit from left...

if x=3482

ans is 8

what is the function for that..
  dipa ahuja replied to kshama parashar
04-Feb-12 03:27 AM
try this:

string x = "3482";
 
string find = x[2].ToString(); //its 8
  Danasegarane Arunachalam replied to kshama parashar
04-Feb-12 12:30 PM
Hope this helps


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
    Dim n As String = "1234"
    'Method one
    MessageBox.Show(n.Substring(2, 1))
    'Method 2
    MessageBox.Show(Mid(n, 3, 1))
 
 
     
 
  End Sub
Create New Account
help
substring in asp.net hi, can any one tell how to use substring to break the text. HI try this using System; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = null; string retString = null; str = "This is substring test"; retString = str.Substring(8, 9); MessageBox.Show(retString); } } } Substring in C# string Class returns a new string that is a substring of this string. The substring begins at the specified given index and extended up to
want to get only 000001 how can i acheive this private void button1_Click( object sender, EventArgs e) { string str = "PO000001" ; string result = str.Substring(2, 6); MessageBox .Show(result); } You can also try this one. . private void btnGetSString_Click( object sender, EventArgs e) { var str = "PO000001" ; string result = str Substring(2, 6); MessageBox .Show(result); } Create New Account
this code not working private void button1_Click(object sender, EventArgs e) { string emailAddress = "me@me.com"; string result = ""; result = emailAddress.Substring(5, 4); if (result = = ".com") { MessageBox.Show("Email Address Ok"); } else { MessageBox.Show("Bad Email Address"); } } hi, If you are using ASP.net then i will suggest you
Error in coding private void button3_Click(object sender, EventArgs e) { string str = null; foreach (object obj in checkedListBox1.CheckedItems) str + = obj.ToString() + ", "; str = str.Substring(0, str.Length-2); int pos = str.LastIndexOf(", "); if (pos ! = 1) { str = str.Remove(pos 1); str = str.Insert(pos, "and"); } MessageBox.Show(str); } in dis code i get exceptions like outofrange and nullreferenceexceptions. . . give any solution. . I last ", " character from your string. If yes then try this. private void button1_Click(object sender, EventArgs e) { string str = string.Empty; foreach (object obj in checkedListBox1.CheckedItems) { str + = obj.ToString() + ", "; } str LastIndexOf(', '); if (pos ! = 1) { str = str.Remove(pos, 1); str = str.Insert(pos, " and "); } MessageBox.Show(str); } Remove "null" assignment from your string " str " variable instead assign it with string.Empty So in that case your "str" contains no value and you are trying to get subString from your "str". . this is causing your OutOfRange Exception . . str = str.Substring(0, str.Length-2); make correction and put condition before doing substring of "str" to check for the length and empty string . . ArugmentOutOfRangeException occurs at:- - str = str
already thank you! Heres the code : / / Variables string ExePath, IniPath; private void Form1_Load(object sender, EventArgs e) { ExePath = Application.ExecutablePath; ExePath = Path.GetDirectoryName(ExePath) + @" \ "; / / Put information out of .ini to array. int I; string[] Seasons = IniToArray("Main", "Seasons"); / / Show in ComboBox for (I = 0; I < Seasons.Length; I++) / / Loop through array and put in combobox. { MessageBox.Show(Seasons[I + 1]); Generalcb.Items.Add(Seasons[I]); } } private string[] IniToArray(string Section, string Key StartChar = 0, EndChar = 0; IniFile Ini = new IniFile(IniPath); StrValue = Ini.IniReadValue(Section, Key); MessageBox.Show(StrValue); MessageBox.Show(StrValue.Length.ToString()); / / Gets every seperate string between ';' from .ini for (I = 0; I < StrValue.Length; I++) { if (StrValue[I] = = ';') { EndChar = I; MessageBox.Show(StrValue.Substring(StartChar, EndChar - StartChar)); IniArray[I] = StrValue Substring(StartChar, EndChar - StartChar); / / Puts string into array. StartChar = I + 1; } } return IniArray; } In the IniToArray