Solved

parsing string / dynamic array

Posted on 2008-10-12
9
836 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 160
Visual Studio editor? 1 38
Resolve Dependency Issues 4 47
Word Directory is not in the drop down list 4 25
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

15 Experts available now in Live!

Get 1:1 Help Now