Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8803759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:25:32+00:00 2026-06-14T01:25:32+00:00

I am trying to split a string into a string[] made of the words

  • 0

I am trying to split a string into a string[] made of the words the string originally held using the fallowing code.

    private string[] ConvertWordsFromFile(String NewFileText)
    {
        char[] delimiterChars = { ' ', ',', '.', ':', '/', '|', '<' , '>','/','@','#','$','%','^','&','*','"','(',')',';'};       
        string[] words = NewFileText.Split(delimiterChars);
        return words;


    }

I am then using this to add the words to a dictionary that keeps up with word keys and their frequency value. All other duplicated words are not added as keys and only the value is affected. However the last word is counted as a different word and is therefore made into a new key. How can i fix this?

This is the code I have for adding words to the dictionary :

public void AddWord(String newWord)
    {
       newWord = newWord.ToLower();
        try
        {
            MyWords.Add(newWord, 1);
        }
        catch (ArgumentException)
        {
            MyWords[newWord]++;
        }

    }

To clarify the problem i am having is that even if the word at the end of a string is a duplicate it is still treated like a new word and therefore a new string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:25:33+00:00Added an answer on June 14, 2026 at 1:25 am

    Split is not the best choice to do what you want to do because you end having this kind of problems and you also have to specify all the delimiters, etc.

    A much better option is using a regular expressions instead of your ConvertWordsFromFile method as follow:

    Regex.Split(theTextToBeSplitted, @"\W+")
    

    This line will return an array containing all the ‘words’. Once you have that, the next step should be create your dictionary so, if you can use linq in your code, the easiest and cleaner way to do what you want is this one:

    var theTextToBeSplitted = "#Hi, this is a 'little' test: <I hope it is useful>";
    var myDictionary = Regex.Split(theTextToBeSplitted, @"\W+")
                            .GroupBy(x => x)
                            .ToDictionary(x => x.Key, x => x.Count());
    

    That´s all that you need.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to split a string with text into words by using the php-function
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I have made a String[] array by using String.split(.) . I am now trying
I'm trying to split a string into sentences (delimited by sentence delimiters). The code
I'm trying to split up a string into two parts using regex. The string
I'm trying to split all the words in a string into an array in
I'm trying to split a string into two different words. I have the following
i'm trying to use php to split a string into array components using either
I am trying to split a Unicode string into words (simplistic), like this: print
I'm trying to split a string into an array of words, however I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.