This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 5 years ago.
I am making a project, But i need to be able to split 1 row of text into 2 strings.
How would i go about doing this?
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Stinger";
Console.Write("Configuration FIle.... ");
Thread.Sleep(1000);
if (System.IO.File.Exists(#"C:\Stinger\Configuration\config.cfg"))
{
Console.Write("Found");
Thread.Sleep(1000);
Console.WriteLine("\r\n");
Console.WriteLine("--Configuration--");
string[] readText = File.ReadAllLines(#"C:\Stinger\Configuration\config.cfg");
foreach (string s in readText)
{
Console.WriteLine(s);
Thread.Sleep(1000);
}
}
else // Configuration File Else Statement
{
Console.WriteLine("Missing");
Thread.Sleep(4000);
}
}
}
And here is my Config Contents
fullscreen 1
I want to be able to make "fullscreen" and "1" part of the row 2 strings.
I've done continuous googling and reading articles on how to split.
But it's not making any sense to me.
Any help?
This is a simple string.Split and is actually very straightforward and fundamental.
Assuming this is the code where you are getting fullscreen 1 from, you can do something like this.
string[] readText = File.ReadAllLines(#"C:\Stinger\Configuration\config.cfg");
foreach (string s in readText)
{
string[] split= s.Split(" ");
Console.WriteLine(split[0]+" "+split[1]);
Thread.Sleep(1000);
}
The Console.WriteLine will print your expected output.
Related
This question already has answers here:
How to change Foreground Color of each letter in a string in C# Console?
(3 answers)
Closed 6 months ago.
So I am trying to color individual charactrs E.G "0 is allways red 1 is allways blue 2 is allways green etc." I am using a nuget package called "MathNet.Numerics.FSharp." And from that i am using BigRational (a BigRational is a infanate bit int.) in a Console.WriteLine E.G "Console.WriteLine(i);" and I am makeing a program with extreamly large numbers and I want to color the numbers to so I can look at the numbers and look for patterns.
The CharacterColoring function that I created can be used both singly and in a loop.
It is used in the loop as follows:
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
public static void CharacterColoring(char YourChar, ConsoleColor YourFavoriteColor)
{
Console.ForegroundColor = YourFavoriteColor;
Console.Write(YourChar);
}
static void Main(string[] args)
{
string YourText = "Hello world 1 2";
for (int i = 0; i < YourText.Length; i++)
{
if (YourText[i] == '1')
{
CharacterColoring(YourText[i], ConsoleColor.Red);
}
else if (YourText[i] == '2')
{
CharacterColoring(YourText[i], ConsoleColor.Blue);
}
else
{
CharacterColoring(YourText[i], ConsoleColor.Green);
}
}
Console.ReadKey();
}
}
}
Tested in:
Visual Studio 2017, .NET Framework 4.5.2, Console App
This question already has answers here:
c# loop until Console.ReadLine = 'y' or 'n'
(2 answers)
Closed 4 years ago.
So I tryed to make a simple text game.And I wanted to do Yes/No dialong I was trying to figure this out for 2 hours, but i didn't find any solution to my problem. I'm total beginner, so I don't know much about coding.
I was trying to achive it trough "bool" bot it wasnt the right option. Obviousli.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TEXTRPG
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Greetings, stranger.");
Console.ReadKey();
Console.WriteLine("I've came here to give you a quest.");
Console.ReadKey();
Console.WriteLine("Do you accept?");
//Yes/No
Console.ReadKey();
Here is my sample
do {
Console.Write("Do you accept? [y/n] ");
response = Console.ReadKey(false).Key;
if (response != ConsoleKey.Enter)
Console.WriteLine();
} while (response != ConsoleKey.Y && response != ConsoleKey.N);
This question already has answers here:
Console.ReadLine("Default Text Editable Text On Line")
(2 answers)
Closed 4 years ago.
I have multiple I/O tasks that I want to do with a console:
Print out standard, non-editable text (Console.WriteLine())
Print out text that the user can edit (?)
Allow the user to type, and be able to output text via the two methods above (?)
It would be nice if I could do password masking too.
Anybody have any solutions?
Edit text like in a console-based text editor?
I think all that you need is in the Console class, have a look at its members:
http://msdn.microsoft.com/en-us/library/system.console.aspx
Maybe you could give curses a try, there is a C# wrapper avaiable. Didn't tried it myself, though...
Party like it's 1988 with Mono's getline. http://tirania.org/blog/archive/2008/Aug-26.html
Answer already submitted
but the below code may be help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static int i = 0;
public static string[] S = new string[] { "A", "B", "C", "D", "E", "F" };
static void Main(string[] args)
{
Console.Write("Please Select : "+S[i]);
ConsoleKeyInfo K = Console.ReadKey();
while (K.Key.ToString() != "Enter")
{
Console.Write(ShowOptions(K));
K = Console.ReadKey();
}
Console.WriteLine("");
Console.WriteLine("Option Selected : " + S[i]);
Console.ReadKey();
}
public static string ShowOptions(ConsoleKeyInfo Key)
{
if(Key.Key.ToString() == "UpArrow")
{
if (i != S.Length-1)
return "\b\b" + S[++i];
else
return "\b\b" + S[i];
}
else if (Key.Key.ToString() == "DownArrow")
{
if(i!=0)
return "\b\b" + S[--i];
else
return "\b\b" + S[i];
}
return "";
}
}
}
The question that I have is regarding converting the process of reading lines from a text file into an array instead of just reading it.
The error in my codes appear at string[] lines = File.ReadLines("c:\\file.txt"); with cannot implicitly convert....
Can someone please advise on the codes to save the results in an array format? I've placed the ReadAllLines code which is able to save the results in an array too. Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace Testing
{
class Analysis
{
static void Main()
{
string[] lines = File.ReadLines("c:\\file.txt");
foreach (string r in lines)
{
Console.WriteLine("-- {0}", r);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
ReadAllLines Codes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace Testing
{
class ReadFromFile
{
static void Main()
{
string[] lines = System.IO.File.ReadAllLines
(#"C:\Users\Public\TestFolder\WriteLines2.txt");
System.Console.WriteLine("Contents of writeLines2.txt =:");
foreach (string line in lines)
{
Console.WriteLine("\t" + line);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
File.ReadLines() returns an object of type System.Collections.Generic.IEnumerable<String>
File.ReadAllLines() returns an array of strings.
If you want to use an array of strings you need to call the correct function.
You could use Jim solution, just use ReadAllLines() or you could change your return type.
This would also work:
System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("c:\\file.txt");
You can use any generic collection which implements IEnumerable, such as IList<String>.
string[] lines = File.ReadLines("c:\\file.txt").ToArray();
Although one wonders why you'll want to do that when ReadAllLines works just fine.
Or perhaps you just want to enumerate with the return value of File.ReadLines:
var lines = File.ReadLines("c:\\file.txt");
foreach (var line in lines)
{
Console.WriteLine("\t" + line);
}
Change string[] lines = File.ReadLines("c:\\file.txt"); to IEnumerable<string> lines = File.ReadLines("c:\\file.txt");
The rest of your code should work fine.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace FileReader
{
class Program
{
static void Main(string[] args)
{
var lines = File.ReadAllLines("D:/Text.txt").ToList();
if(lines != null && lines.Count > 0)
{
foreach(var line in lines)
{
Console.WriteLine(line);
}
}
Console.ReadKey();
}
}
}
I googled and found the solution at MSDN.
// Compose a string that consists of three lines.
string lines = "First line.\r\nSecond line.\r\nThird line.";
// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(lines);
file.Close();
How to extend the lines to complex content which including some natural C# code lines.
eg. I want to write the information below to my test.cs file.
Why?
I am parsing a XML schema with C# Console Application. And i want to generate the Console Result to a .cs file during the compiler time.
using System;
using System.Collections.Generic;
using System.Text;
namespace CommonDef
{
public class CCODEData
{
public int iCodeId;
public string sCode;
public CODEDType cType;
public int iOccures;
}
[Description("CodeType for XML schema.")]
public enum CODEDType
{
cString = 1,
cInt = 2,
cBoolean = 3,
}
thank you.
If your source code is hardcoded as in your sample, you could use a C# literal string:
string lines =
#"using System;
using System.Collections.Generic;
using System.Text;
namespace CommonDef
..."
Anyway in such cases it is a better idea (more readable and maintainable) to have the whole text contents into a text file as an embedded resource in your assembly, then read it using GetManifestResourceStream.
(I'm assuming you're trying to build up the result programmatically - if you genuinely have hard-coded data, you could use Konamiman's approach; I agree that using an embedded resource file would be better than a huge verbatim string literal.)
In your case I would suggest not trying to build up the whole file into a single string. Instead, use WriteLine repeatedly:
using (TextWriter writer = File.CreateText("foo.cs"))
{
foreach (string usingDirective in usingDirectives)
{
writer.WriteLine("using {0};", usingDirective);
}
writer.WriteLine();
writer.WriteLine("namespace {0}", targetNamespace);
// etc
}
You may wish to write a helper type to allow simple indentation etc.
If these suggestions don't help, please give more details of your situation.
I know an answer has already been accepted but why not use an XSLT applied to the XML instead? this would mean that you could easily generate c#, vb.net, .net without having to recompile the app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FileHandling
{
class Class1
{
static void Main()
{
Console.WriteLine("Enter data");
ConsoleKeyInfo k;
//Console.WriteLine(k.KeyChar + ", " + k.Key + ", " + k.Modifiers );
string str="";
char ch;
while (true)
{
k = Console.ReadKey();
if ((k.Modifiers == ConsoleModifiers.Control) && (k.KeyChar == 23))
{
Console.WriteLine("\b");
break;
}
if (k.Key == ConsoleKey.Enter)
{
Console.WriteLine("");
str += "\n";
}
ch = Convert.ToChar(k.KeyChar);
str += ch.ToString();
}
Console.WriteLine(str);
Console.Read();
}
}
}