By the way, to answer your above question - this is to format the length of an MP3 file, so if its only "6" seconds, then yeah, it should look like ":06".
Please find below a version two at the end of this post. I'm chuckling too--I know what you mean about wanting to find shorter code, and for humor (these don't exist but I'm guessing you were thinking the same (OK, it takes 3 lines : - )
Hmmm... I'm wondering if we can build properties--like building .SmartInsert()...
Breaking News - Yes you can. I posted a question about it and someone gave me an answer that with some small changes produced a function .TakeFive() that would subtract 5 from any integer...
Are you working with classes in order to move functions and subroutines into classes (in the App_Code folder) -- if you haven't, you want to -- that works wonders for simplifying code on the code behind page.
Note: I ran into problem with computer errors if the length of the string was 1. However, because this was only going to happen for single digit entries, I decided to do try / catch and if if jumped to the catch part then we could simply have it do what
it needed to do for a single digit result (we would have had to work this into our code anyway--in a way, we turned this into a simplification).
Shawn_Molloy
Member
182 Points
148 Posts
Formatting time string
Feb 27, 2009 10:35 PM|LINK
Hello.
If you a int that can either be 2 - 4 in length, how would you format to look like this
13:45
So, the int can start like this:
12
and should be formatted like this:
:12
or if it start likes this:
112
it should end up like this:
1:12
Thanks!
ldechent
Contributor
6326 Points
1577 Posts
ANSWER
Feb 28, 2009 01:20 AM|LINK
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Molley.aspx.cs" Inherits="Molley" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="InputTextBox" runat="server"></asp:TextBox><br /><br /> <asp:Button ID="Button" runat="server" Text="Format" onclick="Button_Click" /><br /><br /> <asp:Label ID="ResultLabel" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Molley : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button_Click(object sender, EventArgs e) { string originalstring = InputTextBox.Text; int stringlength = originalstring.Length; string firstpart = originalstring.Substring(0, stringlength - 2); ResultLabel.Text = "firstpart " + firstpart; string secondpart = originalstring.Substring(stringlength - 2, 2); ResultLabel.Text = firstpart + ":" + secondpart; } }ldechent
Contributor
6326 Points
1577 Posts
ANSWER -- what we might need to add...
Feb 28, 2009 01:22 AM|LINK
The code was written using C# -- are you a VB person? (Murphy's Law at work here :-)
I can write it in VB.
Also, if someone enters 6 do you want it to be formatted as
:06
?
Shawn_Molloy
Member
182 Points
148 Posts
Re: ANSWER -- what we might need to add...
Feb 28, 2009 03:56 PM|LINK
Hi Larry. Thanks for your reply. I was looking for a C# sample; and this works perfect!
My only wish would be something more elegant, I think this could be done in 2 lines of code. But hey, beggers can't be choosers right? Haha.
Thanks buddy.
-- shawn
Shawn_Molloy
Member
182 Points
148 Posts
Re: ANSWER -- what we might need to add...
Feb 28, 2009 03:58 PM|LINK
public static string FormatTime(string time) { string originalstring = time; int stringlength = originalstring.Length; string firstpart = originalstring.Substring(0, stringlength - 2); string secondpart = originalstring.Substring(stringlength - 2, 2); string result = firstpart + ":" + secondpart; return result; }Shawn_Molloy
Member
182 Points
148 Posts
Re: ANSWER -- what we might need to add...
Feb 28, 2009 03:59 PM|LINK
By the way, to answer your above question - this is to format the length of an MP3 file, so if its only "6" seconds, then yeah, it should look like ":06".
ldechent
Contributor
6326 Points
1577 Posts
Including that zero
Feb 28, 2009 04:44 PM|LINK
Please find below a version two at the end of this post. I'm chuckling too--I know what you mean about wanting to find shorter code, and for humor (these don't exist but I'm guessing you were thinking the same (OK, it takes 3 lines : - )
originalstring = TextBox1.Text;
int length = originalstring.Length;
smartstring = originalstring.SmartInsert(length-2, ":");
Hmmm... I'm wondering if we can build properties--like building .SmartInsert()...
Breaking News - Yes you can. I posted a question about it and someone gave me an answer that with some small changes produced a function .TakeFive() that would subtract 5 from any integer...
Are you working with classes in order to move functions and subroutines into classes (in the App_Code folder) -- if you haven't, you want to -- that works wonders for simplifying code on the code behind page.
Note: I ran into problem with computer errors if the length of the string was 1. However, because this was only going to happen for single digit entries, I decided to do try / catch and if if jumped to the catch part then we could simply have it do what it needed to do for a single digit result (we would have had to work this into our code anyway--in a way, we turned this into a simplification).
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Molley : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button_Click(object sender, EventArgs e) { string originalstring = InputTextBox.Text; string firstpart, secondpart; int stringlength = originalstring.Length; try { firstpart = originalstring.Substring(0, stringlength - 2); ResultLabel.Text = "firstpart " + firstpart; } catch { firstpart = ""; } try { secondpart = originalstring.Substring(stringlength - 2, 2); } catch { secondpart = "0" + originalstring; } ResultLabel.Text = firstpart + ":" + secondpart; } }