Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to delete special character in a string?

Posted on 2008-10-02
3
Medium Priority
?
4,370 Views
Last Modified: 2012-06-27
Hello,
i have the following string : my_string_is_with_underscore,
i'd like to upper the first letter, and replace "_" with " ".

how to do this?
0
Comment
Question by:toshi_
[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
3 Comments
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 270 total points
ID: 22625324
sample code is attached

        static void Main(string[] args)
        {
            String test = "test_score";
 
            test = test.Replace("_", "");
            test = UppercaseFirstFast(test);
        }
 
        public static string UppercaseFirstFast(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return string.Empty;
            }
            char[] letters = name.ToCharArray();
            letters[0] = char.ToUpper(letters[0]);
            return new string(letters);
        }

Open in new window

0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 270 total points
ID: 22626822
to remove the underscore you can use the code suggested by BondInASP
string result = originalString.Replace("_", " ");

to convert the string to title case you can use the following  (copied from http://support.microsoft.com/kb/312890)

Convert a String to Title Case
The String class does not include a method that converts a string to title case. The ToTitleCase method resides in the TextInfo class, which is a member of the System.Globalization namespace. Unlike the ToUpper and ToLower methods of the String class, the ToTitleCase method is not a static method and requires an instance of the class.

When you use the TextInfo class, you must specify cultural information. In most situations, you can default to the culture that is currently in use. Culture information is a property of the thread on which the code is running. To obtain the culture information, you must gain access to the current thread and retrieve the CurrentCulture property from that thread. Once you accomplish this, you can create the TextInfo object. For example:
CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
                        
The TextInfo class also includes the ToUpper and ToLower methods. Use these methods of TextInfo if you need to specify culture options.
    Console.WriteLine(textInfo.ToTitleCase(title));
    Console.WriteLine(textInfo.ToLower(title));
    Console.WriteLine(textInfo.ToUpper(title));
                        
If you need to create or manipulate strings that have specific culture settings, you can use one of the overloaded constructors of the TextInfo class to create strings with any of the available culture options.

0
 
LVL 23

Assisted Solution

by:Tony McCreath
Tony McCreath earned 210 total points
ID: 22632302
I don't think any of the previous solutions do the upper case to each word, just the first one. This code may help
string text = "my_string_is_with_underscore";
 
string[] words = text.Split('_');
 
for (int i = 0; i < words.Length; i++)
{
    string word = words[i];
    if (word.Length > 1) // word
        words[i] = char.ToUpper(word[0]) + word.Substring(1);
    else if (word.Length > 0) // single character
        words[i] = word.ToUpper();
    // else empty string
}
 
string newText = String.Concat(words);

Open in new window

0

Featured Post

Independent Software Vendors: 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 Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

604 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