CSharp/C# >> Given an int n, how to return n tabs?

by Author » Sun, 07 Sep 2008 08:31:56 GMT

I thought that this might be really simple, but I just can't figure
out how.

So, I wanna create a method that takes an int n, and the method return
n concatenated "\t"s.

i.e.,

public string GetTabs(int n)
{
if (n == 1)
{ return "\t"; }
else if (n == 2)
{ return "\t\t"; }
else if (n==3)
{ return "\t\t\t"; }
else if (n == 4)
{ return "\t\t\t\t"; }
...
...
}

I know in python, this is pretty simple, you can simple say

mystring = "\t";
myNewString = mystring * n;

And myNewString would have n "\t"s concatenated.

Is there anything similar like this in C#?

Thanks.

CSharp/C# >> Given an int n, how to return n tabs?

by Peter Duniho » Sun, 07 Sep 2008 08:46:17 GMT



Hint: when you're trying to figure out how to do something with a specific
type, a great starting place is to review the class members. It requires
a bit of clicking through on MSDN when you're dealing with overloaded
methods, but one can usually narrow the search down just by ignoring
things obviously not pertinent.

For any sort of string initialization issue, the very first place you
should look is the String class constructors. :)

As for the specific question...


Well, the explicit version would be something like this:

StringBuilder sb = new StringBuilder(n);

for (int i = 0; i < n ; i++) { sb.Append('\t'); }

string str = sb.ToString();

But it turns out that the String class has a constructor just for this:

string str = new string('\t', n);

As well, there is a StringBuilder overload that does the same thing:

StringBuilder sb = new StringBuilder(n);

sb.Append('\t', n);

string str = sb.ToString();

I'm guessing the string constructor is what would be most useful to you,
but hopefully all of the above is helpful.

Pete

CSharp/C# >> Given an int n, how to return n tabs?

by Registered User » Sun, 07 Sep 2008 08:54:45 GMT

On Sat, 6 Sep 2008 17:31:56 -0700 (PDT), Author < XXXX@XXXXX.COM >


return String.Empty.PadRight(n, '\t');

CSharp/C# >> Given an int n, how to return n tabs?

by Andrew » Sun, 07 Sep 2008 11:19:10 GMT

I liked it Peter... true... with the habit of writing strings
day-in-and-day-out with

String thisWorld = "HelloWolrd";
...
String partialWorld = thisWorld.Substring....;

(pseudo code)

we tend to take classes like String for granted and forget to look at the
other 'less used' facilities they offer...

thanks for reminding not to forget the basics...

-Andrew

CSharp/C# >> Given an int n, how to return n tabs?

by Author » Sun, 07 Sep 2008 11:29:15 GMT


Haha, immediately after I posted this question, I realized how stupid
a question it was, so I managed to have removed it from google groups,
but the news servers were fast enough to have propagated. Thank you
guys anyway.

Quite often I notice that the attempt itself to post a question in a
news group helps resolve the question. Often times, the time I finish
writing up the question, I've already got the answer.

CSharp/C# >> Given an int n, how to return n tabs?

by Peter Duniho » Sun, 07 Sep 2008 15:30:49 GMT


For what it's worth, a much better approach to that situation is to simply
follow-up your own post with the answer.

First, there are no stupid questions. While it's great if even beginners
can answer some questions themselves with the documentation, if you're
stumped, you're stumped. So, you can bet that if you had the question,
someone else will at some point and having your question present in the
newsgroup archive will help them, especially if you've provided an answer
to go with it.

Second, and just as relevant, canceling a post is extremely unreliable
these days. People have been abusing the post-canceling feature of Usenet
for some time now, and many ISPs simply ignore it. Google cheats a bit by
making things work fine for their own users, but not necessarily
supporting Usenet more broadly. So to you, it sure looks like you
canceled the post, but you really haven't.

Since you're not really canceling the post when you think you are, you
might as well post the answer instead. That way it saves others the
trouble of posting the answer. :)

Pete

Similar Threads

1. Help needed: LoadStr() returns "Tab position incompatible with current tab style"

I wanted to add language swap functionallity to my app as in the Richedit 
example(ex\Apps\Richedit) in Borland C++ 6.0. In the OnCreate event of the 
main form the following code is executed to read the current language 
setting from a Pascal unit:

...OnCreate(...)
{
        AnsiString    LangIDasText ;
        int               LangIDasInt ;

        LangIDasText = LoadStr(_SLcid.id);
        LangIDasInt = LangIDasText .ToInt();
}

What I understand from docs, is that _SLcid is a preprocessor macro that the 
Pascal compiler (DCC32) generates for every resource string when it 
generates the header file. The application can use those macro's at runtime.
The funny thing is that the code worked to start with in my example app 
(RichEdit) and not in my program. After a cleanup of RichProg(removed all 
files except for source, forms, headers and project files), I now get the 
exact message as with my program.

A Google search comes up with:
Consts_SInvalidTabPosition, "Tab position incompatible with current tab 
style"

I found no further explainations on the web. Who knows what causes the 
problem? 


2. window (int, int, int, int) in conio.h library - VJSharp/C#

3. int main(int argc, char* argv[]) & int main(int argc, char** argv)

Hi,

I have a question. I created a simple executable program using Visual C++ 
from Visual Studio 6.0

This program is called from a script that passes in one argument.

Now, my question is:

1. When I use int main(int argc, char* argv[]) declaration, the argv usually 
contains the data and some garbage at the end.

For example: a script calls this executable and passes in data => this-is-me
When it executes this C++ program, the argv contains => this-is-me@#

Where did the @# come from? and Why?

2. When I use main(int argc, char** argv) declaration, the argv is exactly 
what the script passes it.

For example: a script calls this executable and passes in data => this-is-me
When it executes the C++ program, the argv contains exactly => this-is-me

So, what is the difference between these two declarations? Why does the 
first one contains garbage characters?

Please help me to understand. I am fairly new at Visual C++.

Many Thanks

4. int main(int argc, char* argv[]) & int main(int argc, char** a

5. Give me an Int

int.Parse() OR Convert.ToInt32() OR (int)

Hi, 

Is there any sort of convention as to when its appropriate to use 
int.Parse(), Convert.ToInt32() or casting to int? 

At the moment I am using them interchangeably. If one doesnt work, i try 
another one. 

I know that int.Parse is for creating an int from a string, but for 
Convert.ToInt32() would do the same thing.

What are real life instances when one should be preferred over the other.?


thank you..

6. int overflow gives UB

7. Get the int value of a given character in VB.NET under PPC

Let's say, I have "abc", I want to get "efg". What function shall I use? 
Many thanks.

8. Web Method giving me an error on return - CSharp/C#