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
222 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reading the Web.Config using IIS 7.5? 4 49
Keyboard 2 41
2 questions 10 29
C# MVC Insert Multiple Row into DB 2 32
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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