Solved

Regex C# Using it to make string "Material_Load_Table" to "Material Load Table"

Posted on 2013-06-18
4
322 Views
Last Modified: 2013-06-19
Hello!

I've ran into a problem today with regex.

I would like to use a regex expression to turn "Material_Load_Table" and other similair looking sentences into "Material Load Table"

So that would be, replacing the underlines with space and turn the first letter in every word into uppercase.

(I want to solve this in one expression and I dont really want to use loops or anything else)

I've managed somehow to do something similair http://jsfiddle.net/bLm44/2/ but I am not happy with the way the code looks and it is buggy.


Any idea what I can do? Appreciate a quick answer! Ask me if there is anything unclear :)
0
Comment
Question by:clooak
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:johny_bravo1
ID: 39257194
Hi,

//Remove all the underscores
   string inputString = "Material_load_Table";
        Regex re = new Regex("_");
        string outputString = re.Replace(inputString, " ");
        string YourRequiredString = UppercaseWords(outputString);

Open in new window



//For converting starting word to capital
public static string UppercaseWords(string value)
    {
        char[] array = value.ToCharArray();
        // Handle the first letter in the string.
        if (array.Length >= 1)
        {
            if (char.IsLower(array[0]))
            {
                array[0] = char.ToUpper(array[0]);
            }
        }
        // Scan through the letters, checking for spaces.
        // ... Uppercase the lowercase letters following spaces.
        for (int i = 1; i < array.Length; i++)
        {
            if (array[i - 1] == ' ')
            {
                if (char.IsLower(array[i]))
                {
                    array[i] = char.ToUpper(array[i]);
                }
            }
        }
        return new string(array);
    }

Open in new window

0
 
LVL 8

Accepted Solution

by:
johny_bravo1 earned 500 total points
ID: 39257202
For Uppercase use can use ToTitleCase
Pls Refer,
http://www.dotnetperls.com/totitlecase
0
 

Author Closing Comment

by:clooak
ID: 39257474
Thanks your link,lead me ultimately to my solution which ended up looking
     
public string ToTitleCase(string inputString)
        {  
            inputString = inputString.Replace("_", " ");
            System.Globalization.TextInfo txtInfo = System.Globalization.CultureInfo.CurrentCulture.TextInfo;
            return txtInfo.ToTitleCase(inputString.ToLower());
        }
0
 

Author Comment

by:clooak
ID: 39258595
For anyone in the future, heres what my function finally looked like, you need to add "using System.Globalization" in the top.


 public string ToTitleCase(string inputString)
        {
            return (CultureInfo.CurrentCulture.TextInfo.ToTitleCase(inputString.ToLower().Replace("_", " ")));
        }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

895 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

15 Experts available now in Live!

Get 1:1 Help Now