+ 1

How to put a non static Method into a String- .Split()

I try to make a little console application in C#, where I have a given text, which cuts the part, you gave in the input (Console.ReadLine()), out. If you wonder: I am going to make a switch statement, in case the user input, cannot be found in the given text. Heres my Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ForTests { class Program { class SplitText { public static string text = "You can cut here any word you want in this text. ANY. So do it. Now."; // The uncut text in string private string input; // The methods which are carring the .ReadLine input public string Input { get { return input; } set { input = value; } } string[] words = text.Split(Input); // Here is the text.Split, where the Input method does not work public void Write() // Here is the method to assemble the text back together { for (int i = 0; i < words.Length; i++) { Console.Write(words[i]); } } public void t() //This Method shows the given text in the console { Console.WriteLine(text); } } static void Main(string[] args) { SplitText s = new SplitText(); s.Input = Console.ReadLine(); // Here I defined Input as Console.ReadLine() Console.WriteLine(": "); s.t(); // This shows the uncut text Console.WriteLine("Cutted text: "); s.Write(); // Here you get the cutted text Console.ReadKey(); } } }

15th Sep 2017, 7:16 PM
Dan Gelorde
Dan Gelorde - avatar
2 Antworten
+ 3
string[] words = text.Split(Input); This line is ran when the SplitText object is created (SplitText s = new SplitText();) in Main, not when the input string value is set. So when it is ran input has no value. Also the Split method takes a char[], or string[] as a parameter, not a String. https://stackoverflow.com/questions/8928601/how-can-i-split-a-string-with-a-string-delimiter So you may need to re-think your approach. I.E. get rid of the input property, or change its type to a string[] and add to it from the object. Wrap the error line in a method of its own, which means you'd need to also extract the words variable out of the method and make it a property as well so that the Write method has access to it, or move the line into the Write method. and so on.....
15th Sep 2017, 8:38 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thanks for your Answer! I have now reworked my Code a little on this base: class Program { static void Main() { string value = "cat\r\ndog\r\nanimal\r\nperson"; // Split the string on line breaks. // ... The return value from Split is a string[] array. string[] lines = Regex.Split(value, "\r\n"); foreach (string line in lines) { Console.WriteLine(line); } } } And came with this Solution and now it works! My Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace ForTests { class Program { class SplitText { public static string text = "You can cut here any word you want in this text. ANY. So do it. Now."; public string input; // stores user input public void Input() // method to take a user input { input = Console.ReadLine(); } public void Write() // takes input as a reference, what it should split and reassembles it then { string[] words = Regex.Split(text, input); for (int i = 0; i < words.Length; i++) { Console.Write(words[i]); } } public void t() //This Method shows the given text in the console { Console.WriteLine(text); } } static void Main(string[] args) { SplitText s = new SplitText(); Console.WriteLine("Uncutted text: "); s.t(); // This shows the uncut text s.Input(); // takes the user input Console.WriteLine("Cutted text: "); s.Write(); // Here you get the cutted text Console.ReadKey(); } } }
15th Sep 2017, 9:09 PM
Dan Gelorde
Dan Gelorde - avatar