Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I print different length of spaces in C#?
Write(number*" ");
doesn't work here.
EDIT: But it works in Python.
Indeed, there's no * operator taking a string and an integer.
The simplest option is probably to use the string(char, int) constructor:
Write(new string(' ', number));
Depending on your actual use case, you might want to look at string.PadLeft and string.PadRight too, if you're actually trying to pad an existing string.
You can do it like that:
Write(new String(' ', number)); // <- ' ' character, number times
That's, probably the simplest way. You may also want to look String.Format esp. if you try to print out a padded data. For instance:
// Prints out myData padded left up to 10 characters
Write(String.Format("{0,10}", myData));
Try with this snippet:
int index = 0;
while(index<number)
{
System.Console.Write(" ");
index++;
}
You can encapsulate this in a wrapper function, and call it as
printblank(number);
using:
void printblank(int number)
{
int index = 0;
while(index<number)
{
System.Console.Write(" ");
index++;
}
}
Thereby you can use this function repeatedly, as per your requirement.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Currently very new to C# and coding , so i will be more than happy if someone will explain me how to display how many digits the number has. For example the number 12345 has 5 digits.the main theme in the class is while loops so the answer probably need to contain while loop.TY
You can either use this
Math.Abs(myint).ToString().Length
and if you absolutely must use a while loop then
number = Math.Abs(number);
int length = 1;
while ((number /= 10) >= 1)
length++;
To test code
string.Trim().Replace("-","").Length
so if you have a number you should make it a string first using ToString()
The Length returns the number of characters that you hold within your string minus your white spaces (Because of the Trim()),i don't see why you would want to use the while loop in the first place.
Edit : if you have a minus number the .Replace() will take care of that.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Currently, I have something like the following:
public char functName(int n)
{
some functionality....
if(condition1)
return convertFuncToChar(variable) + functName(modifiedNumber);
else
return convertFuncToChar(variable);
}
however, I realize that doesn't give me a string (and the syntax shows that there's an error).
I know that for c++, I would most likely use char* to initialize the function, but this is C#.
I don't think it works if I initialize with String either.
Assuming I understand the question correctly, you can take advantage of two things:
1) all basic types implement .ToString(), which creates a string for you
2) string implements operator+()
The following is an example of a recursive function that will recursively concatenate characters to create a string. The output is the number in reverse as a string.
static string funcName(int n)
{
if (n<10)
return (n%10).ToString();
return (n%10).ToString() + funcName(n/10);
}
Of course, it would be more efficient to write this non-recursively.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I don't have much code to show but I am trying to remove a substring (individual number) from a longer string is a series of numbers.
Ex: Main String is "11,12,15,16,55,33,88,100,121,155,115"
Need to find number 16 and remove it from the string leaving...
11,12,15,55,33,88,100,121,155,115
They are a list of id's from a database so I can't just change them to strings. Also how do I remove it as if it wasn't there?
string numbers = "11,12,15,16,55,33,88,100,121,155,115";
numbers = string.Join(",", numbers.Split(',').Where(num => num != "16"));
But why don't you use a List<int> instead for database ID's?
In this specific use case I would split the string using "," as the separator, remove the element that matches, then join the elements again.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example I have this string:
BTW This is a comment "hahaha"
BTW is a comment operator and all statements after it are ignored.
I need to put BTW as 'comment_operator' and 'This is a comment "hahaha"' as 'comment' in a datagridview.
But I can't do it because I used space as a delimiter in my code, so 'This is a comment "hahaha"' will be concatenated too but I need it as it is.
Can someone enlighten me with this? Thanks.
I assume you want the separate 2 parts by the first occurrence of space. You can use the code below:
string text = #"BTW This is a comment ""hahaha""";
string comment_operator = text.Substring(0, text.IndexOf(' '));
string comment = text.Substring(comment_operator.Length + 1);
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm in the middle in studying some code and I encountered this word "Append" and I don't understand what it does.
Code:
public static void appendData(string data)
{
if (isRecording) sb.Append(data + Environment.NewLine);
}
What does append mean?
The answer from ChrisF is correct as far as StringBuilder.Append is concerned.
In general, the word "Append" means "to add to the end of". See http://en.wiktionary.org/wiki/append.
I would point out that the "right" way to do that bit of code is:
public static void appendData(string data)
{
if (isRecording)
{
sb.Append(data);
sb.Append(Environment.NewLine);
}
}
Append is doing the same job as string1 + string2 but it is doing it in a much more efficient manner. Look up "Immutable Strings C#" for some more details if you need them.
This is quite simple. Then code above is simply "adding" or "appending" the variables/text supplied within the brackets to the variable "sb".
Append can be found as part of the System.Text.StringBuilder class which I believe is being used above.
More info can be found following this link: StringBuilder Class
Happy coding!
I would guess that sb is of type StringBuilder.
Append() adds the supplied string to the end of the string being built in the StringBuilder variable.
It will add the string representation of the object to end of the string builder instance. It basically calls the .ToString() method of whatever object you pass in and concatenates it to the end of the internal string being build up.
See MSDN documentation
Assuming you are using Visual Studio put your cursor on the word Append and Press F1, you'll probably see something like this. If you are considering refactoring this and assuming it is using a StringBuilder, you might also want to read about AppendLine.