-1

I'm completely lost with what is happening here.

string send = "!points add " + entries[winner] + " " + prize.ToString();

What I want to send is "!points add winnername prizeamount" but what I get is "!points add winnername\nprizeamount". I put \n because it writes a new line but trying to replace "\n", "\r" and "\t" with " " does nothing. enter image description here

all I need is the message to be exactly"!points space add space winnername space prizeamount space"

If it's important the entries in my code is a List of strings

Negomir
  • 13
  • 1
  • 5
  • 4
    Given the code you have shown, if there is a `\n` character in `send` then the origin is either one of the strings in `entries` or in `prize.ToString()`. – Igor Apr 27 '18 at 10:25
  • and looking at your provided image, your `entries[winner]` is the one which has a linebreak at its end. You should remove this either here or when filling the list. – casiosmu Apr 27 '18 at 10:27
  • 2
    How did you try to remove newlines? `String.Replace` works. What you posted here doesn't show any attempt to remove newlines. – Panagiotis Kanavos Apr 27 '18 at 10:28
  • I don't know about entries, there shouldn't be anything to add a new line in it, and prize is an int so idk why that would have a new line. – Negomir Apr 27 '18 at 10:29
  • BTW post *text* not images, and actual code that reproduces the issue. It's impossible to guess from looking at an image whether there's a space after `negomir99` or not, or whether there's a `\n` or `\r\n` – Panagiotis Kanavos Apr 27 '18 at 10:29
  • send = send.Replace("\n", " "); send = send.Replace("\r", " "); send = send.Replace("\t", " "); – Negomir Apr 27 '18 at 10:30
  • @Negomir we can't know either, since you don't provide any data. We don't know what `entries` contains or what `prize` is. The only thing certain is that there are no ghosts. One of your values contains a newline at least. Or you are printing the string in a UI control that wraps the output text. – Panagiotis Kanavos Apr 27 '18 at 10:31
  • There is no space after negomir99 – Negomir Apr 27 '18 at 10:31
  • @Negomir post the *actual code and data* in the question itself. It's impossible to guess right now. There are no ghosts. If there were, thousands of developers would have found them already. BTW you could replace this code with `String.Format("!points add {0} {1}",entries[winner],prize);`. The result is the same but it's cleaner and *clearly* shows that if there are newlines, they are in the data. It also allows you to format individual parameters – Panagiotis Kanavos Apr 27 '18 at 10:32
  • OK so context, entries contains twitch usernames of people who have entered a raffle sort of thing, prize is an integer set in twitch chat in the command !candyhunt 50 for example, and after making the send string I do irc.SendToChat(Send); – Negomir Apr 27 '18 at 10:34
  • @Negomir no context please. Code. Context doesn't compile. You should be able to add just 3 lines to create a reproducible exampl, eg: `var entries=new List{"A", "B", "C"}; int prize=90; int winner=2;`. And one more line to check the result, `Trace.Assert("!points add A 90",send)`; – Panagiotis Kanavos Apr 27 '18 at 10:34

3 Answers3

0

The entries strings already contain the new line character(s).

I suggest you replace with Environment.NewLine: Replace Line Breaks in a String C#

dpant
  • 1,622
  • 19
  • 30
  • This can't be the correct answer. The image in the question shows no space before the number on the second line. If your answer was correct, there would be a space because of the `... + " " + prize`. – DodgyCodeException Apr 27 '18 at 10:41
  • @DodgyCodeException images don't compile. You can't be certain this isn't a wrapped line, or that it doesn't end in a *space* and newline. Besides, there's nothing wrong with string formatting or concatenation. If there are newlines, they are in the parameters – Panagiotis Kanavos Apr 27 '18 at 10:42
0

The string object, 'entries[winner]' is having line-feeds (LF) or carriage-returns (CR). You try this to remove all LFs and CRs,

string send = "!points add " 
+ entries[winner].Replace("\r", string.Empty).Replace("\n", string.Empty) 
+ " " + prize.ToString();

Alternatively, you can use Trim() to remove leading\ trailing LFs\ CRs.

string send = "!points add " 
+ entries[winner].Trim() 
+ " " + prize.ToString();
Ashokan Sivapragasam
  • 2,033
  • 2
  • 18
  • 39
  • This can't be the correct answer. The image in the question shows no space before the number on the second line. If your answer was correct, there would be a space because of the `... + " " + prize`. – DodgyCodeException Apr 27 '18 at 10:40
  • @DodgyCodeException why not? The line may be ending in a *space* and newline. You can't tell from the image. What's absolutely certain though is that string concatenation works – Panagiotis Kanavos Apr 27 '18 at 10:43
  • @PanagiotisKanavos that's not possible because even if the first line ended in a space and then a newline, the second line would still be a space and a number. A number.toString() cannot possibly contain a newline. Therefore the newline must be something that was added after the statement in question, possibly as a means of doing a wraparound by the display method. – DodgyCodeException Apr 27 '18 at 10:58
  • @DodgyCodeException again, there are no ghosts. String concatenation doesn't introduce characters. An image of a string is *not* the string. And what looks like a space, especially something copy-pasted from a web page, can contain non-visible characters. The OP claims something that's impossible. – Panagiotis Kanavos Apr 27 '18 at 11:33
-2

No repro. The following code doesn't assert.

var entries=new List<string>{"Aaa", "Bbb", "Ccc"}; 
int prize=90;
int winner=1;

var send=String.Format("!points add {0} {1}",entries[winner],prize);
var send2="!points add " + entries[winner] + " " + prize.ToString();

Trace.Assert("!points add Aaa 90"==send);
Trace.Assert(send2==send);

If the result contains newlines, it's because the entries values contain newlines.

The best solution would be to clean the input data before storing it in the list, eg with String.TrimEnd or String.Trim, When loading data from a file for example, you can't be sure it doesn't contain trailing spaces.

To read clean data from a file you could use :

var entries=File.ReadLines()
                   .Select(line=>line.Trim())
                   .ToList();

If you add the entries one by one from user input :

entries.Add(newEntry.Trim());

If you can't change how the data is read (why?) you can trim when whenever you use an entry value:

var send=String.Format("!points add {0} {1}",entries[winner].Trim(),prize);

Loading clean data is a lot easier

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • The thing is - no matter what you try to put in `entries[winner]`, you will never succeed in having `"\n50"` as a substring of `send`. Which is what the image in the question shows. – DodgyCodeException Apr 27 '18 at 11:04
  • @DodgyCodeException did the OP post such an example? An image does *not* compile. String concatenation *never* introduces characters. Given the information on the original question you can *prove* that it can't be reproduced. Whatever the OP's problem is, it's not string concatenation. – Panagiotis Kanavos Apr 27 '18 at 11:29
  • @DodgyCodeException besides the point isn't to find what string combination produces the impossible result claimed by the OP. The OP needs to understand that there's nothing wrong with concatenatino and find the **real** problem – Panagiotis Kanavos Apr 27 '18 at 11:32
  • What I'm trying to say is there may not be a problem at all. As you say, the OP claims something that's impossible. So if he claims there is a newline, there's no point in assuming there's a newline. It's more probable that the newline is created by the display method (to wrap the text around). So there's no point in trimming `entries` unless the OP can *prove* there is a newline. – DodgyCodeException Apr 27 '18 at 11:38
  • @DodgyCodeException yes. Which was the point of this answer. To actually prove to the OP that if there are newlines, they are in the data. To actually make him/her understand that he/she *must* post enough code to reproduce the issue – Panagiotis Kanavos Apr 27 '18 at 11:38
  • No, I think the newline is added *after* the string `send` gets created. – DodgyCodeException Apr 27 '18 at 11:40