Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format instead of Substring

I have a number like 6348725, displayed as String, but I want to display only the last 6 digits (348725) with String.Format. Is there a way to do this? I know, there is the Substring Function (or Int calculation with % (Mod in VB)), but I want to use a FormatString by a User Input from a TextBox.

like image 971
ubsch Avatar asked Oct 20 '22 23:10

ubsch


1 Answers

I don't understand why you need String.Format but you can use it like;

string s = "6348725";
TextBox1.Text = s.Substring(s.Length - 6)); // Textbox will be 348725

Okey, I just wanna show a dirty way without Substring;

string s = "6348725";
var array = new List<char>();
if (s.Length > 6)
{
    for (int i = s.Length - 6; i < s.Length; i++)
    {
        array.Add(s[i]);
    }
}

Console.WriteLine(string.Join("", array)); // 348725
like image 80
Soner Gönül Avatar answered Nov 04 '22 02:11

Soner Gönül