String [] control

ok so it's bad to cut the chase here. and to be clear, im looking for code examples where possible.

So, I have a normal line, say

string mystring = "this is my string i want to use";

      

ok, now that I have my string I have split it by space with

string[] splitArray = mystring.Split(new char[] { ' ' });

      

ok so now I have splitArray [0] via splitArray [7]. now, i need to do some fancy string stuff that i usually shouldn't. here are a few:

I need to cut off the first word, so I am left with 7 other words, so I have something like:

string myfirstword = "this";
mystring = "is my string i want to use";

      

now, I will need to use mystring over and over, using different parts of it at different times, and depending on the string, I will not imagine how long it will take. so I will give some examples of what you need.

firstly, it hurts to know how many words there are (it's easy, just throwing it) and secondly, it hurts to somehow use things like

string secondword = splitArray[1];
string everythingAfterTheSecondWord = splitArray[2+];

      

if you notice, I've included [2 +] ... + indicating that I want all the lines in the array to fit together, spaces in all, on a string. eg,

string examplestring = "this is my example for my Qaru question";
string[] splitArray2 = examplestring.Split(new char[] { ' ' });

      

now if I called splitArray2 [4+] I would need to return "for my question". now it is obvious that it is not as easy as adding a + to the string array .. but this is what I need and in the current situation I have tried many other simple ways that just do not work.

ALSO, if I were to call something like splitArray2 [2-5], I would like words 2 through 5, obviously.

Summary: I need more control over my string [] arrays, and I need to be able to find every word after the * word in order to be able to strip out random words in the string while leaving the rest of the string intact, and I need to be able to find the string m

throughn

Thank!

+2


source to share


2 answers


Most of what you are looking for can be achieved with List<string>

. In short:

string mystring = "this is my string i want to use";

List<string> splitArray = new List<string>(mystring.Split(new char[] { ' ' }));
string firstWord = splitArray[0];

// mystring2 = "is my string i want to use"
splitArray.RemoveAt(0);
string mystring2 = String.Join(" ", splitArray.ToArray());

      

The more complex tasks you describe with splitArray[2+]

require LINQ, albeit therefore .NET 3.5.



List<string> everythingAfterTheSecondWord = splitArray.Skip(2).ToList();

      

For splitArray[2-5]

:

List<string> arraySlice = splitArray.Skip(2).Take(3).ToList();

      

+4


source


Okay, to do "every word starting with an X", you could do this:

string newString = string.join(splitArray," ",x);

      

To get the words y starting with x, do the following:

string newString = string.join(splitArray," ",x,y);

      



To get the word count:

int wordCount= splitArray.Length;

      

Putting it all together, the xy words look like this:

string newString = string.join(splitArray," ",x, splitArray.Length-x+1);

      

0


source







All Articles