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

  • Home
  • SEARCH
  • 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 8995813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:40:10+00:00 2026-06-15T23:40:10+00:00

Just a curiosity I was investigating. The matter: simply repeating (multiplying, someone would say)

  • 0

Just a curiosity I was investigating.

The matter: simply repeating (multiplying, someone would say) a string/character n times.
I know there is Enumerable.Repeat for this aim, but I was trying to do this without it.
LINQ in this case seems pretty useless, because in a query like

from X in "s" select X  

the string “s” is being explored and so X is a char. The same is with extension methods, because for example “s”.Aggregate(blablabla) would again work on just the character ‘s’, not the string itself. For repeating the string something “external” would be needed, so I thought lambdas and delegates, but it can’t be done without declaring a variable to assign the delegate/lambda expression to.
So something like defining a function and calling it inline:

( (a)=>{return " "+a;} )("a");  

or

delegate(string a){return " "+a}(" ");  

would give a “without name” error (and so no recursion, AFAIK, even by passing a possible lambda/delegate as a parameter), and in the end couldn’t even be created by C# because of its limitations.
It could be that I’m watching this thing from the wrong perspective. Any ideas?
This is just an experiment, I don’t care about performances, about memory use… Just that it is one line and sort of autonomous. Maybe one could do something with Copy/CopyTo, or casting it to some other collection, I don’t know. Reflection is accepted too.

  • 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-15T23:40:11+00:00Added an answer on June 15, 2026 at 11:40 pm

    If you’re trying to repeat a string, rather than a character, a simple way would be to use the StringBuilder.Insert method, which takes an insertion index and a count for the number of repetitions to use:

    var sb = new StringBuilder();
    sb.Insert(0, "hi!", 5);
    Console.WriteLine(sb.ToString());
    

    Otherwise, to repeat a single character, use the string constructor as I’ve mentioned in the comments for the similar question here. For example:

    string result = new String('-', 5); // -----
    

    For the sake of completeness, it’s worth noting that StringBuilder provides an overloaded Append method that can repeat a character, but has no such overload for strings (which is where the Insert method comes in). I would prefer the string constructor to the StringBuilder if that’s all I was interested in doing. However, if I was already working with a StringBuilder, it might make sense to use the Append method to benefit from some chaining. Here’s a contrived example to demonstrate:

    var sb = new StringBuilder("This item is ");
    sb.Insert(sb.Length, "very ", 2) // insert at the end to append
      .Append('*', 3)
      .Append("special")
      .Append('*', 3);
    Console.WriteLine(sb.ToString()); // This item is very very ***special***
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a stupid question. But just out of curiosity, is there
Just for curiosity/convenience: C# provides two cool conditional expression features I know of: string
Just for curiosity I would like to know how to do this in the
This is just a curiosity question. I know that Spring can be used in
Is there any way (just out of curiosity because I came across multiple same-value
Just a question for my own curiosity. I have heard many times that it's
Just a curiosity question. Remember when in class groupwork the professor would divide people
Just out of curiosity, for applications that have a fairly complicated module tree, would
Just for curiosity... It's possible to know the timestamp of an object creation in
This is just a curiosity question that I didn't know how to search via

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.