Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want the output of the code to be:
hi
hi
hi
hi
hi

but instead its hihihihihi

What I have tried:

C#
a = "hihihihihi";

system.WriteLine(a, "\n")
# it didn't work
Posted
Updated 17-Jul-18 12:20pm
v4
Comments
[no name] 17-Jul-18 17:04pm    
Can you be more specific. Is it that you want to split the text in 2 letters chunks and display in separate lines?
Eric Lynch 17-Jul-18 17:26pm    
Also, the suggested code wouldn't even compile in C#. I'm guessing you meant var a = "hihihihihi"? or string a = "hihihihihi"? Also, I'm guessing you meant Console.WriteLine not System.WriteLine?

Assuming the guesses are correct, there are two problems. First, you are using Console.WriteLine improperly.

The overload that takes a string argument and a variable number of additional arguments is for formatting strings. In this case, the first argument provides the format and the remaining arguments provide the parameters to be used with that format.

For example, Console.WriteLine("Number={0}", 123) would write out the text "Number=123" followed by a new line character (WriteLine adds a new line onto whatever it writes out).

The second problem (noted by kmn1235) is that you seem to expect the WriteLine method to (somehow) magically separate your string. It will not do this.

For comparison, the following code would provide the output you hope for:

Console.WriteLine("hi");
Console.WriteLine("hi");
Console.WriteLine("hi");
Console.WriteLine("hi");
Console.WriteLine("hi");

Although, I would probably put that in a loop as follows:

for(int index = 0; index < 5; index++)
Console.WriteLine("hi");
j snooze 17-Jul-18 17:38pm    
you have to split the "hihihi" up before you can put them on separate lines.
If you are trying to put text together to output onto a text file with carriage returns you can concatenate Environment.NewLine on your string as well, otherwise refer to previous answers if you are trying to output to console.

1 solution

To get this result
hi
hi
hi
hi
hi

you need to output
hi\nhi\nhi\nhi\nhi

in 1 go or 5 times
hi\n

but since your code is
C#
a = "hihihihihi";

you need to split the variable as indicated in comments.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900