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
226 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 50 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

695 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