Solved

Parse and split a variable into 3 strings

Posted on 2007-04-04
1
232 Views
Last Modified: 2010-04-15
I have a string like this format....    2:30 PM and I want to parse into 3 different variables. So to the left of the colon : is one variable, then the next 2 digits is another variable and the last 2 is the final variable. What is the best way to parse this?
0
Comment
Question by:chicabow
[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
1 Comment
 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 50 total points
ID: 18850413
Hi Chicabow,

One way to do this is like this:

            string str = "2:30 PM";
            string[] split = str.Split(": ".ToCharArray());
            for(int i = 0; i < split.Length; i++)
               Console.WriteLine(i + " --> " + split[i]);

in this case,
  split[0] = 2
  split[1] = 30
  split[2] = PM

You could assign these to your own vars however you want.

Cheers

Wint
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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