?
Solved

Given a variable length string (11:15 OR 9:30), how do I use Substring to pull out the first number?

Posted on 2006-06-12
3
Medium Priority
?
228 Views
Last Modified: 2010-04-16
This works if the hour is 11:15
myTime.Substring(0,2)

This works if the hour is 9:30
myTime.Substring(0,1)

How do I make it so it works with either string? I don't know ahead of time how long the string is.
0
Comment
Question by:M3hcSS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
neilprice earned 200 total points
ID: 16889362
Assuming you have the colon everytime you can use;

myString.Substring(0, myString.IndexOf(':'));

For example see below;

using System;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = "9:30";
            string s2 = "11:55";

            string s3 = s1.Substring(0, s1.IndexOf(':'));
            string s4 = s2.Substring(0, s2.IndexOf(':'));

            Console.WriteLine(s3);
            Console.WriteLine(s4);
            Console.ReadLine();
        }
    }
}
0
 
LVL 2

Author Comment

by:M3hcSS
ID: 16889550
That's actually a really freaky answer.
0
 
LVL 2

Author Comment

by:M3hcSS
ID: 16894051
To get the minutes to the right side of the colon...

JUNKTimeOn1.Substring((JUNKTimeOn1.IndexOf(":")+1),2)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question