Solved

parsing string / dynamic array

Posted on 2008-10-12
9
835 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now