471,330 Members | 1,090 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Given an int n, how to return n tabs?

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.
Sep 7 '08 #1
5 850
On Sat, 06 Sep 2008 17:31:56 -0700, Author <gn********@gmail.comwrote:
I thought that this might be really simple, but I just can't figure
out how.
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...
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"; }
...
...
}
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
Sep 7 '08 #2
On Sat, 6 Sep 2008 17:31:56 -0700 (PDT), Author <gn********@gmail.com>
wrote:
>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)
{
return String.Empty.PadRight(n, '\t');
>}
Sep 7 '08 #3
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
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sat, 06 Sep 2008 17:31:56 -0700, Author <gn********@gmail.comwrote:
>I thought that this might be really simple, but I just can't figure
out how.

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...
>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"; }
...
...
}

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

Sep 7 '08 #4
On Sep 6, 11:19*pm, "Andrew" <ex...@noemail.noemailwrote:
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

"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...
On Sat, 06 Sep 2008 17:31:56 -0700, Author <gnewsgr...@gmail.comwrote:
I thought that this might be really simple, but I just can't figure
out how.
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...
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"; }
* *...
* ...
}
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
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.
Sep 7 '08 #5
On Sat, 06 Sep 2008 20:29:15 -0700, Author <gn********@gmail.comwrote:
[...]
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.
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
Sep 7 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dan Jacobson | last post: by
1 post views Thread by Bill Gates | last post: by
135 posts views Thread by Xah Lee | last post: by
22 posts views Thread by santosh | last post: by
7 posts views Thread by =?Utf-8?B?aXdkdTE1?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.