• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Formating a String

I have a string similar to the one shown below. I want first letter of every word to be capitalized (otherwise lowercase), everything before "/" will still be capitalized.

TET/TEST TEST TESTING

whats the best method of doing this?
0
pfslink
Asked:
pfslink
  • 3
  • 3
  • 2
  • +2
1 Solution
 
planoczCommented:
Are these Directories or Text files or what?
0
 
pfslinkAuthor Commented:
just a string passed from the db
0
 
planoczCommented:
Here is a sample using Regular Expressions.....


Imports System.Text.RegularExpressions
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        UseRegularExpression("TET/TEST TEST TESTING")
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
        Me.Text = "Form1"
    End Sub

#End Region

    Private Sub UseRegularExpression(ByVal strName As String)
        Dim regExp As New Regex("([a-zA-Z]{1,})\/{1}\s{0,}([a-zA-Z]{1,})\s{0,}([a-zA-Z]{0,})")
        Dim matchGroups As GroupCollection = regExp.Match(strName).Groups
        Dim sDir As String

        sDir = UCase(matchGroups(1).Value) & "/"
        sDir += UCase(matchGroups(2).Value.Substring(0, 1)) & LCase(matchGroups(2).Value.Substring(1))
        sDir += UCase(matchGroups(3).Value.Substring(0, 1)) & LCase(matchGroups(3).Value.Substring(1))

        MsgBox(sDir)

    End Sub
End Class
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Bob LearnedCommented:
 Dim testString As String = "TET/TEST TEST TESTING"

  Dim slashPos As Integer = testString.IndexOf("/")

  Dim properCase As String = StrConv(testString.Substring(slashPos + 1), VbStrConv.ProperCase)
  Dim upperCase As String = testString.Substring(0, slashPos).ToUpper

Bob
0
 
dharmesh_amityCommented:
I guess best thing to do here is use string and String.Split() method. I have never used string tokenizer it but you can look up.

I got this piece of code from http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=28

private void Page_Load(object sender, System.EventArgs e)
{
   
    // string seperated by colons ';'
    string info = "mark;smith;123 csharp drive;toronto;canada";

    string[] arInfo = new string[4];
   
    // define which character is seperating fields
    char[] splitter  = {';'};
           
    arInfo = info.Split(splitter);

    for(int x = 0; x < arInfo.Length; x++)
    {
        Response.Write(arInfo[x] + "<br>");
    }

}


Hope that is helpful
0
 
pfslinkAuthor Commented:
the program is in vb.net
0
 
muzzy2003Commented:
Haven't tested this, I mostly use C#, but should do the trick.

Dim delimiter As String = "/"
Dim delimiterChars As Char() = delimiter.ToCharArray()
Dim parts As String()
Dim myTI As TextInfo = New CultureInfo("en-US", False).TextInfo

parts = testString.Split(("/").ToCharArray())
testString = parts[0] + "\" + myTI.ToTitleCase(parts[1])
0
 
muzzy2003Commented:
Sorry - change that to:

Dim delimiter As String = "/"
Dim delimiterChars As Char() = delimiter.ToCharArray()
Dim parts As String()
Dim myTI As TextInfo = New CultureInfo("en-US", False).TextInfo

parts = testString.Split(delimiterChars)
testString = parts[0] + "\" + myTI.ToTitleCase(parts[1])
0
 
dharmesh_amityCommented:
Just need to change the syntax for VB.NET as muzzy2003 suggested
0
 
pfslinkAuthor Commented:
where is TextInfo defined?

how about parts[0], what is this referring to?
0
 
muzzy2003Commented:
It is defined in System.Globalization - add an "Imports" for this to the top of the code. parts[0] refers to the first part of the array formed in the previous line - basically, everything before the \.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now