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
217 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
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
neilprice earned 50 total points
Comment Utility
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
Comment Utility
That's actually a really freaky answer.
0
 
LVL 2

Author Comment

by:M3hcSS
Comment Utility
To get the minutes to the right side of the colon...

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now