[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Split and swap string

Posted on 2005-05-09
6
Medium Priority
?
177 Views
Last Modified: 2010-04-23
If I have the following string:

Saturday 12:23am  

I want to change it to:

12:23am Saturday

Swapping the two fields.

If just the time is present:

12:23am    

don't do anything.

thanks
0
Comment
Question by:Richard Kreidl
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 1000 total points
ID: 13961829
       Dim sX() As String = inputstring.Split(" ")
        Array.Reverse(sX)
        outputstring = String.Join(" ", sX)
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 13961853
The preceding will work for arbitrary space delimited strings - since reversing a singleton array does nothing, an absence of a space will produce nothing.

Obviously, if you want error checking (require only two words, make sure of legitimate days of week, etc.), you can do that before you call Array.Reverse.
0
 
LVL 8

Expert Comment

by:doobdave
ID: 13961914
Hi rkckjk,

You can try the following:

Private Function ParseDate(pDateToParse as String) as String

Dim strSplitString() as String
strSplitString = Split(pDateToParse, " ") ' This function splits the string into an array of strings, splitting them on the specified character (" ", one space, in this case)

If strSplitString.Length = 2 Then
  Return strSplitString(1) & " " & strSplitString(0)
ElseIf strSplitString.Length =1 Then
  Return strSplitString(0)
End If

End Function

However, if you are working solely with dates, there are several useful functions for doing such things.
Have a look at the MSDN for "DateTime Structure"

HTH,
David
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:doobdave
ID: 13961932
oops, sorry jens,
I was writing that while you must've posted!

rkckjk, Jensfiederer's solution is more elegant (using the array.reverse() & string.join() functions - which I had forgotten about!) so I would go with that.

Regards,
David
0
 
LVL 2

Author Comment

by:Richard Kreidl
ID: 13962991
It swaps the string, but maybe I didn't pose my question well.

I only want the string swapped if the data is entered incorrectly:

Incorrectly entered:
Saturday 12:23am

Correctly enetered:
12:23am Saturday

0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 13963234
Then instead of a simple
     Array.Reverse(sX)
you can do
        If sX.Length > 1 Then
            If sX(1).Length > 0 Then
                If Char.IsDigit(sX(1).Chars(0)) Then
                    Array.Reverse(sX)
                End If
            End If
        End If
0

Featured Post

Technology Partners: 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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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.
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

834 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