Backslash n "\n" doesn't work? - c#

Hello I am making an application to split the song text based on certain char. The application works when I type something such as "sampletext/ sampletext", then the result should be :
sampletext
sampletext
but, instead of above, the result is
sampletextsampletext
Below is the code :
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = "";
string[] a = TextToSongFormatConverter(textBox1.Text, '/');
textBox2.Text += "\n"; textBox2.Text += "\n"; textBox2.Text += "\n";
foreach (var item in a)
{
textBox2.Text += item;
textBox2.Text += "\n";
}
}
public static string[] TextToSongFormatConverter(string text, char separator)
{
string[] result;
result = text.Split(separator);
for (int i = 0; i < result.Length; i++)
{
result[i] = result[i].Trim();
}
}
I am using visual studio 2012 and c# and windows form :
This is the screenshoot :
Any idea why? Thanks.

Use Environment.NewLine instead. It will give you proper new line characters for system your program is running on.
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
from Environment.NewLine Property

Try using Environment.NewLine instead of \n.
MSDN

Related

Remove Dot from String

Trying to remove a Dot from a String i´m experiencing a problem. With different Chars such as "," or "-" its working properly but the Dot won´t disappear.
Got a TextBox with the Name nr_tb. I Input Hello.World and my MessageBox outputs the same. Changing the Dot in the Replace function with a "," works.
Not working:
string line;
private void nr_tb_TextChanged(object sender, TextChangedEventArgs e)
{
line = nr_tb.Text.Replace(".","");
MessageBox.Show(line);
}
Working:
string line;
private void nr_tb_TextChanged(object sender, TextChangedEventArgs e)
{
line = nr_tb.Text.Replace(",","");
MessageBox.Show(line);
}
string number= "2.36";
string newNumber = number.Replace(".", "");
It might be another character that seems like a DOT. Char code of DOT is 46 so if you get this char code in your string you can easily Replace(".", ""); it.
This method show each char code of a string:
private static string DotRemover(string temp)
{
string _Result = string.Empty;
foreach (byte a in Encoding.UTF8.GetBytes(temp.ToCharArray()))
{
_Result += a.ToString() + Environment.NewLine;
}
return _Result;
}
OR
you can replace everything that look likes a DOT :
public static string CleanNumb(string numb)
{
foreach (char c in ".,'´")
numb = numb.Replace(c, ' ');
return numb.Replace(" ", "");
}

Weird spacing in Hex to String

I am trying to make a hex to string converter and for some reason the spacing between bytes in the conversion is multiplied by 2.
I would like it to spit out a single space between characters,
private void button2_Click(object sender, EventArgs e)
{
try
{
textBox1.Clear();
textBox2.Text = textBox2.Text.Replace(" ", "");
string StrValue = "";
while (textBox2.Text.Length > 0)
{
StrValue += System.Convert.ToChar(System.Convert.ToUInt32(textBox2.Text.Substring(0, 2), 16)).ToString();
textBox2.Text = textBox2.Text.Substring(2, textBox2.Text.Length - 2);
textBox1.Text = textBox1.Text + StrValue + " ";
}
}
catch (Exception ex)
{
MessageBox.Show("Conversion Error Occurred : " + ex.Message, "Conversion Error");
}
}
so "41 41" converted would look like "A A", but this is what happens:
image
Does anybody see what I am doing wrong?
In this line
textBox1.Text = textBox1.Text + StrValue + " ";
you consequently append the result of calculations to your TextBox1.
So, after the first iteration the result is A, you append it and a whitespace to TextBox1.
Then, you take the second 41 and convert it. Now, StrValue is AA and you append it and space to TextBox1, and so on.
You need to move this line out of your while loop:
textBox1.Clear();
textBox2.Text = textBox2.Text.Replace(" ", "");
string StrValue = "";
while (textBox2.Text.Length > 0)
{
StrValue += System.Convert.ToChar(System.Convert.ToUInt32(textBox2.Text.Substring(0, 2), 16)).ToString();
textBox2.Text = textBox2.Text.Substring(2, textBox2.Text.Length - 2);
}
textBox1.Text = StrValue;
As some people mentioned in comments, you need to stop working with TextBoxes this way. It is pretty confusing. You may want to do the following:
private string HexToString(string hex)
{
string result = "";
while (hex.Length > 0)
{
result += Convert.ToChar(Convert.ToUInt32(hex.Substring(0, 2), 16));
hex = hex.Substring(2); // no need to specify the end
}
return result;
}
Then, in your button click event or wherever else:
textBox1.Text = HexToString(textBox2.Text.Replace(" ", ""));
As simple as that. Or you can even move replacing the whitespaces in the method. Now, this code is readable and is logically separated.
The problem seems to be caused by the accumulated value in StrValue. You should define that variable inside your while, and assign it only (don't append a new value).
while (textBox2.Text.Length > 0)
{
string StrValue = System.Convert.ToChar(System.Convert.ToUInt32(textBox2.Text.Substring(0, 2), 16)).ToString();
textBox2.Text = textBox2.Text.Substring(2, textBox2.Text.Length - 2);
textBox1.Text = textBox1.Text + StrValue + " ";
}

How to display a converted string

I'm working on an assignment which wants me to have the user input a string with no spaces and to identify each word by having it start with a capital letter, so "IAmName" would convert to "I Am Name". I think I have that part down, my problem is the last step, which is displaying the new string in a label, here is my code so far:
private string ConvertText()
{
string str = inputTextBox.Text;
if (str.Contains(" "))
{
MessageBox.Show("No spaces allowed");
}
string newstring = outputLabel.Text;
for (int i = 0; i < str.Length; i++)
{
if (char.IsUpper(str[i]))
newstring += " ";
newstring += str[i].ToString();
}
return newstring;
}
Any help would be greatly appreciated.
Just do it the other way around:
outputLabel.Text = ConvertText(); // Or any other label that should display it
I'm not sure why you have this (string newstring = outputLabel.Text;) inside your code though. You're not using it, you just overwrite it.
You might also want to use a StringBuilder to concatenate in a loop, it's a lot more efficient.
And this code probably should add a return;
if (str.Contains(" "))
{
MessageBox.Show("No spaces allowed");
return; // Return so it stops executing this method
}
I edited you sample a little
I think it correct as below.
public static class MyClass
{
public static string ConvertText(string inputText)
{
var newstring = "";
for (var i = 0; i < inputText.Length; i++)
{
if (char.IsUpper(inputText[i]))
newstring += " ";
newstring += inputText[i].ToString(CultureInfo.InvariantCulture);
}
return newstring;
}
}
string output = MyClass.ConvertText("MyNameIsBlaBla");
Console.WriteLine(output);
" My Name Is Bla Bla"

How to count characters in RichTextBox using C#

I am new to programming and having problems making a simple code for character counter for a button. I've somehow managed to code a line together that actually counts words. here it is:
private void button_add_Click(object sender, EventArgs e)
{
string count = richTextBox.Text;
label_info.Text = "Word count is " + (count.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Length).ToString();
//Trying to add a new character count down here
// label_info.Text = "Character count is " + ....code ...;
}
Any advice would be appreciated.
EDIT: Thanks to you all i got my answer here:
private void button_add_Click(object sender, EventArgs e)
{
string count = richTextBox.Text;
label_info.Text = "Word count is " + (count.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Length).ToString();
label_info.Text = "\nCharacter count is " + richTextBox.Text.Lenth;
}
Try using string's Length property.
label_info.Text = label_info.Text + "\nCharacter count is " + richTextBox.Text.Lenth;
You are splitting the string on each space and counting the length of the resulting array. If you want the length of the string (with whitespace etc.) you can do this:
label_info.Text = "Character count is " + count.length;
If you don't want the whitespace you can do this:
label_info.Text = "Character count is " + count.Replace(" ", "").length;
You don't have to write a complex code. You can count characters using length property. See below sample codes. In it we use a Textbox and a Label, Label shows the count of characters entered in the textbox.
void textBox1_TextChanged(object sender, EventArgs e)
{
lablename.Text = textboxname.Text.length.Tostring();
}
I have also created its video tutorial. Watch it on YouTube
string a = richTextBox1.Text;
label2.Text = a.Length.ToString();

Multiple character casing in same TextBox

How to change character casing in TextBox? I need that 1 line character been Upper and second line character benn Lower
isv.CharacterCasing = CharacterCasing.Upper;
isv.Text = "Upper"
isv.CharacterCasing = CharacterCasing.Lower;
isv.Text = "Lower"
As Mark said, it's difficult to understand exactly what you need, but I think it's something like
string[] lines = isv.Text.Split('\n');
string finalText = string.Empty;
for (int i = 0; i < lines.length; i++)
finalText += i%2==0 ? lines[i].ToUpper() : lines[i].ToLower() + + Environment.NewLine;
isv.Text = finalText;
Keep in mind I wrote the code without the compiler :)
You can use TextBox.Lines property I guess.
something like:
private void button1_Click(object sender, EventArgs e)
{
string result = string.Empty;
result += textBox1.Lines[0].ToUpper() + Environment.NewLine;
result += textBox1.Lines[1].ToLower();
textBox1.Text = result;
}
isv.Text = isv.Text.Split(Environment.NewLine)[0].ToUpper() + isv.Text.Split(Environment.NewLine)[1].ToLower();

Categories