.Net/C# - Convert Char[] to String

.NET / C# - Convert char[] to string

There's a constructor for this:

char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'};
string s = new string(chars);

C#: Converting List of Chars to String

One option for this is to use the string constructor:

var myString = new string(array1.ToArray());

convert a char array to string and use it as label text

.NET / C# - Convert char[] to string

string completelpt = new String(lpt)
lpt_label.(completelpt);

cannot convert from 'char[]' to 'string[]'

Use a different method:

System.IO.File.WriteAllText(ofd.FileName, richTextBox1.Text);

I'd recommend avoiding to use Lines for this; it's a waste of resources to split the text into N strings only to write them all back to a combined file

Why didn't your first attempt work? WriteAllLines requires an array of strings. If you call .ToArray() on a string you get an array of characters; strings and characters are very different things.

ToArray() is a LINQ method that works because a string can be treated as an enumerable sequence of char. Typically it's very rare that you would do so and you would probably use the dedicated string.ToCharArray() method if you did want a char array from a string. Mostly I use it when splitting strings on multiple characters: someString.Split(".,?!'-/:;()".ToCharArray()); as it's more readable than putting each char separately

You're more likely to use ToArray() later on for things like filtering one array using LINQ and turning the result into an array again:

Person[] smiths = people.Where(person => person.LastName == "Smith").ToArray();

Other points:

OpenFileDialog's FileName is already a string; you don't need to ToString() it

Please get into the habit of renaming your controls after you add them to a form (top of the properties grid, the (Name) line, takes two seconds). It's a lot easier for us on the internet (and you in 3 weeks' time) to be able to read usernameTextBox and go "oh, that's the username textbox" than it is to be wondering "is it textbox56 or textbox27 that is the username? I'll just go into the form designer and check.."

Char array to a String using .NET system?

What type is VarInput? If it's a string initially, just remove the ToCharArray() call and you can write it to a file directly with File.WriteAllText.

File.WriteAllText(path, VarInput);

Once you have a char array, you don't have to convert to a string in order to write to a file. You can also write bytes directly.

var bytes = System.Text.Encoding.UTF8.GetBytes(characters);
File.WriteAllBytes(path, bytes);

Unassigned local variable and converting char to string

The error is here: char value = PhoneCharToNumber(value);

where value argument for PhoneCharToNumber hasn't been declared nor assigned.

I think you wanted to use string value = PhoneToNumber(str); instead.
Infact your user enters number inside inputTextBox; you have to convert that text to a valid number and then place the result to resultsLabel.

private void CheckButton_Click(object sender, EventArgs e)
{
string str = inputTextBox.Text;
string value = PhoneToNumber(str);
resultsLabel.Text = value;
}

cannot convert char to string CS0030

First you have to add a new Instance of var openFileDialog = new OpenFileDialog(); if you don't have. Then you could do something like this to get the text of the file and add it to you listbox:

private void button5_Click(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
var file = openFileDialog.FileName;
var text = File.ReadAllText(file);
fastColoredTextBox.Text = text;
var filename = Path.GetFileName(file);
listBox1.Items.Add(filename);
}
}

Also you do not have to do this:

foreach (string file in files)

because Path.GetFile() returns only a string with your FileName. So you can add it directly to your ListBox.



Related Topics



Leave a reply



Submit