473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 907
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Jacobson | last post by:
In wdg-html-reference/html40/block/pre.html there is no mention about how ASCII tabs (^I) are to be rendered. I suppose all bets are off? Wait, my docs go thru html-tidy first, and it says...
1
by: Bill Gates | last post by:
Hello, I am creating oblique tabs for a webapp. It needs to be oblique tabs, and not regular tabs that you see in web browsers. Here's how it looks like: http://auriance.com/docs/interaction/ As...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
22
by: santosh | last post by:
I've written the following function to return a string of arbitrary length from stdin. So far, it seems to work as it should. Are there any unportable assumptions and/or logical errors in the code?...
6
by: Stephen Edwards | last post by:
I am a novice, so bear with me. I designed a website using CSS TABS, all was fine, then I copy and pasted meta name, etc for keywords, tweaking it where necessary for keywords. I did not notice...
7
by: =?Utf-8?B?aXdkdTE1?= | last post by:
hi, im attempting to create an intellisense type form and have hit a roadblock. I need the form to return (like the ShowDialog method returns the dialog result) the value that was selected when the...
5
by: wayniac | last post by:
I have a database built on 4 tabs, but inside one of those tabs I would like another group of tabs. The problem is that my second group of tabs are being displayed on ALL of the main 4 tabs and not...
7
by: Phil Reynolds | last post by:
I'm using a tab control in Access 2000, and the user requested to have buttons in the form header, instead of the built-in tabs (so that when they scroll down, they can still switch tabs). Now,...
5
by: dangt85 | last post by:
Hello, I have the following page: ... <style type="text/css"> body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.