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 9017715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:20:41+00:00 2026-06-16T04:20:41+00:00

I have a string as listed below. string sample = class0 .calss1 .class2 .class3.class4

  • 0

I have a string as listed below.

string sample = ” class0 .calss1 .class2 .class3.class4 .class5 class6 .class7″;

I need to create a list of WORDS from this sample string.

A WORD is a string that starts with a period and ends with:

  1. a space or
  2. another period or
  3. end of string

Note: The key point here is – the splitting is based on two criteria – a period and a blank space

I have following program. It works fine. However, is there a simpler/more efficient/concise approach using LINQ or Regular Expressions?

CODE

        List<string> wordsCollection = new List<string>();
        string sample = " class0 .calss1 .class2 .class3.class4  .class5 class6 .class7";

        string word = null;

        int stringLength = sample.Length;
        int currentCount = 0;

        if (stringLength > 0)
        {
            foreach (Char c in sample)
            {

                currentCount++;
                if (String.IsNullOrEmpty(word))
                {
                    if (c == '.')
                    {
                        word = Convert.ToString(c);
                    }
                }
                else
                {

                    if (c == ' ')
                    {
                        //End Criteria Reached
                        word = word + Convert.ToString(c);
                        wordsCollection.Add(word);
                        word = String.Empty;
                    }
                    else if (c == '.')
                    {
                        //End Criteria Reached
                        wordsCollection.Add(word);
                        word = Convert.ToString(c);
                    }
                    else
                    {
                        word = word + Convert.ToString(c);
                        if (stringLength == currentCount)
                        {
                            wordsCollection.Add(word);
                        }
                    }
                }

            }
        }

RESULT

        foreach (string wordItem in wordsCollection)
        {
            Console.WriteLine(wordItem);

        }

enter image description here

Reference:

  1. Splitting up a string, based on predicate
  2. Is there a better way to get sub-sequences where each item matches a predicate?
  3. Linq based generic alternate to Predicate<T>?
  • 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-16T04:20:42+00:00Added an answer on June 16, 2026 at 4:20 am

    You can do this with a regular expression.

    Code

    Regex regex = new Regex(@"\.[^ .]+");
    var matches = regex.Matches(sample);
    string[] result = matches.Cast<Match>().Select(x => x.Value).ToArray();
    

    See it working online: ideone

    Result

    .calss1
    .class2
    .class3
    .class4
    .class5
    .class7
    

    Explanation of Regular Expression

    \.      Match a dot
    [^. ]+  Negative character class - anything apart from space or dot (at least one)
    

    Related

    • Character Classes or Character Sets
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four string as listed below. Though they have different order of characters
I have created the query listed below. It works when I run it. string
I have the global listed below that I'm trying to create a couple of
I have string as abvd.qweqw.sdfs.a=aqwrwewrwerrew . I need to parse this string and get
I have a class, the outline of which is basically listed below. import org.apache.commons.math.stat.Frequency;
I have a MySQL singleton class written in PHP. Its code is listed below:
I have a webdav function listed below: The behavior is completely unexpected.... When I
I have two of the following java classes (listed below) Class BookInfo declares static
I have the following code to encrypt a value (listed below). Now I would
I have the following class, listed below with static field of arrays. Could you

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.