Solved

parsing string / dynamic array

Posted on 2008-10-12
9
838 Views
Last Modified: 2013-12-17
have a string for example:

string MySentence;

What I want to be able to do is parse that sentence and seperate all the words/entries that are seperated by a space into a seperate mini string/char array..

so,  "this is my first sentence." would become:

[1]  "this"
[2]  " "
[3]  "is"
[4]  " "
[5]  "my"
[6]  " "
[7]  "first"
[8]  " "
[9]  "sentence."

the reason I mention dynamic is that it is not possible to determine how many mini chunks are needed in advance -- depends what is contained in the string initially.

how is type of variable truncating / dynamic array scripted in c# / .net based pages?
0
Comment
Question by:amillyard
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Accepted Solution

by:
kukno earned 240 total points
ID: 22698028
       string sample = "this is my first sentence.";
        string[] words = sample.Split(' ');
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }

Cheers
Kurt
0
 
LVL 10

Assisted Solution

by:kukno
kukno earned 240 total points
ID: 22698040
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 40 total points
ID: 22698132
After seeing what you actually need is not completely provided by string.split() method, but what it gives is the dynamic array but not including the spaces as needed by you

I recommend you to use the string.split() method but just keep in mind that every array member will be followed by a space character. Its just a programming logic and can be implemented quite easily
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 220 total points
ID: 22698267
I am doing a little change to the code by kukno

string sample = "this is my first sentence.";
string sample1 = sample.Replace(" ", "* *");
        string[] words = sample1.Split('*');
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }
0
 

Author Comment

by:amillyard
ID: 22698363
ok, got it -- works fine as written.

utilising one the example (via the url link provided).

is there a way to still have the 'chars' that have been stripped away to be kept?

i.e. ' '   '.'   ','  etc... to be recorded on the next line (like the words seperated.
string inputValue = "this is my first sentence.";
 
            string[] inputWords = 
                inputValue.Split(new char[] { ' ', ',', ';', '.', '-', '/', '|', '\\',
                                              '%', '#', '@', '!', '~', '$', '^', '(',
                                              ')', '+', '[', ']', '{', '}', '"', ':', 
                                              '<', '>', '?',  '\r', '\n', '*' }, StringSplitOptions.RemoveEmptyEntries);
 
            foreach (string inputWord in inputWords)
            {
                Console.WriteLine(inputWord);
            }

Open in new window

0
 

Author Comment

by:amillyard
ID: 22698391
checking through :  ragi0017  &  TechTiger007 submissions (did notice were there, as page was not refreshed in time -- when I submitted my response to kukno
0
 

Author Comment

by:amillyard
ID: 22698406
TechTiger007:  what is the addiitonal value provided with your update please?   i.e. replacing the ' ' with '*' etc.
0
 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 220 total points
ID: 22698438
the space is replaced with *space* and then split the content with * so that you will not loose the space and you will get the data as you wanted

[1]  "this"
[2]  " "
[3]  "is"
[4]  " "
[5]  "my"
[6]  " "
[7]  "first"
[8]  " "
[9]  "sentence."
0
 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 220 total points
ID: 22698451
I used * assuming that your data will not contain * in it. You can use any character or character sequence in place of * only thing to be considered is that your regular data should not consist of this sequence
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question