Solved

parsing string / dynamic array

Posted on 2008-10-12
9
839 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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