0

I'm trying to create a program that splits a string to an array then adds to that array.

Splitting the string works but adding to the array is really putting up a fight.

//here i create the text
string text = Console.ReadLine();
Console.WriteLine();

//Here i split my text to elements in an Array 
var punctuation = text.Where(Char.IsPunctuation).Distinct().ToArray();
var words = text.Split().Select(x => x.Trim(punctuation));

//here i display the splitted string
foreach (string x in words)
{
  Console.WriteLine(x);
}

//Here a try to add something to the Array
Array.words(ref words, words.Length + 1);
words[words.Length - 1] = "addThis";

//I try to display the updated array
foreach (var x in words)
{
  Console.WriteLine(x);
}

//Here are the error messages |*error*|
Array.|*words*|(ref words, words.|*Length*| + 1);
words[words.|*Length*| - 1] = "addThis";

'Array' does not contain definition for 'words'

Does not contain definition for Length

Does not contain definition for length */

Tiago Silva
  • 2,299
  • 14
  • 18
  • 3
    https://stackoverflow.com/questions/1440265/how-to-add-a-string-to-a-string-array-theres-no-add-function and https://stackoverflow.com/questions/1126915/how-do-i-split-a-string-by-a-multi-character-delimiter-in-c there are tons of answers for your two questions. It took less than 30 seconds – Kevin Oct 17 '19 at 07:16
  • 2
    How did you come up with using `Array.words`? – Chetan Oct 17 '19 at 07:16
  • I think you mean `Array.Resize`, not `Array.words` – Andrew Williamson Oct 17 '19 at 07:16
  • 2
    Possible duplicate of [How to add a string to a string\[\] array? There's no .Add function](https://stackoverflow.com/questions/1440265/how-to-add-a-string-to-a-string-array-theres-no-add-function) – Kevin Oct 17 '19 at 07:17
  • Always use the right tool ! Arrays are not made for this; use List ! – TaW Oct 17 '19 at 07:19
  • My reason for doing this is to complete a school project. This code worked though. string[] resize = new string[] { "January", "February", "Marts", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; Array.Resize(ref resize, resize.Length + 1); resize[resize.Length - 1] = "addThis"; foreach (var x in resize) { Console.WriteLine(x); } – Coding to beat Goku Oct 17 '19 at 07:31

2 Answers2

2

Convert the IEnumerable to List:

var words = text.Split().Select(x => x.Trim(punctuation)).ToList();

Once it is a list, you can call Add

words.Add("addThis");
Andrew Williamson
  • 8,299
  • 3
  • 34
  • 62
joehoper
  • 404
  • 3
  • 5
0

Technically, if you want to split on punctuation, I suggest Regex.Split instead of string.Split

  using System.Text.RegularExpressions;

  ...

  string text = 
    @"Text with punctuation: comma, full stop. Apostroph's and ""quotation?"" - ! Yes!";

  var result = Regex.Split(text, @"\p{P}");

  Console.Write(string.Join(Environment.NewLine, result));

Outcome:

Text with punctuation      # Space is not a punctuation, 3 words combined
 comma
 full stop
 Apostroph                 # apostroph ' is a punctuation, split as required
s and 
quotation



 Yes

if you want to add up some items, I suggest Linq Concat() and .ToArray():

    string text = 

    string[] words = Regex
      .Split(text, @"\p{P}")
      .Concat(new string[] {"addThis"})
      .ToArray();

However, it seems that you want to extract words, not to split on puctuation which you can do matching these words:

  using System.Linq;
  using System.Text.RegularExpressions;

  ...

  string text = 
    @"Text with punctuation: comma, full stop. Apostroph's and ""quotation?"" - ! Yes!";

  string[] words = Regex
    .Matches(text, @"[\p{L}']+") // Let word be one or more letters or apostrophs
    .Cast<Match>()
    .Select(match => match.Value)
    .Concat(new string[] { "addThis"})
    .ToArray();

  Console.Write(string.Join(Environment.NewLine, result));

Outcome:

Text
with
punctuation
comma
full
stop
Apostroph's
and
quotation
Yes
addThis
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215