473,480 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 908
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
3404
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
2232
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
7336
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
1887
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
1760
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
15094
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
19136
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
3732
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
8657
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
7046
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
6908
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
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6741
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
5342
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,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.