Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:37:22+00:00 2026-06-12T12:37:22+00:00

I want to add a method to string that convert space char to underscore

  • 0

I want to add a method to string that convert space char to underscore (extension method), I deployed the code but why it doesn’t work?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "moslem";
            string.SpaceToUnderScore(name);
        }

        public static string SpaceToUnderScore(this string source)
        {
            string result = null;
            char[] cArray = source.ToArray();
            foreach (char c in cArray)
            {
                if (char.IsWhiteSpace(c))
                {
                    result += "_";
                }
                else
                {
                    result += c;
                }
            }
            return result;
        }
    }
}

Why doesn’t it work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:37:23+00:00Added an answer on June 12, 2026 at 12:37 pm

    First put your extension method to a static class then invoke as name.SpaceToUnderScore()

    var newstr = "a string".SpaceToUnderScore();
    
    
    public static class SomeExtensions
    {
        public static string SpaceToUnderScore(this string source)
        {
            return new string(source.Select(c => char.IsWhiteSpace(c) ? '_' : c).ToArray());
            //or
            //return String.Join("",source.Select(c => char.IsWhiteSpace(c) ? '_' : c));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Expanding on recent_posts_on_self below, I want to add an all_recent_posts_on_self method but I'm not
Is there a standard macro to convert a char string literal that is substituted
I want to call a method that takes in a string for each selected
I want to add a custom model method to the admin filter, however it
In my web application I want to add authorization in certain action method in
The method Picture receives a random int i . I want to add i
Greetings, I have a method to capture packets. After capturing I want to add
I wrote this n-array tree class. I want to write a method to add
I want add UIGestureRecognizerDelegate to UIWebView,but failed. if [self.view addsubView:webView]; So UIWebView is ok,but
I want to show icon that associated with file type. Something like this But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.