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 */

6

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

1 Comment

think ill try with array first but i appreciate the answer !
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

1 Comment

thanks a lot ! im gonna look into Regex and Concat to understand this better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.