.NET C# stringtok equal to Java.

Hi Expert:

I am creating a string with a list of words and each word is to be separated by a comma (name1,house,doe,truck,work etc...).

I don't know actually how many words will be in the list until the end of the function.

I worked something up ( stringvar+= ", ") to put a comma in between each word.

But in the case when function ends with only one word in the list , I get a comma after that one word which is not what I want!

I know I can strip the comma off the one word string at the end of function but how do the experts do this whole thing?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I usually add the comma if its not the last item in the loop like:

string strtemp = "";
for (int i = 0;i<10;i++)
  strtemp += i.ToString();
  if (i < 10)
     strtemp += ",";  

For a start I would use the System.Text.Stringbuilder to build up your string - I think I'm right when I say that it is a LOT more memory efficient if you are concatenating a lot of strings together. The System.String is immutable; i.e. methods that appear to modify it just return a new instance of it. The StringBuilder, however, is mutable.

Essentialy stringvar += "newword," creates an entirely new string in memory each time, it doesn't just add the new part to the end of the existing one - baaaaaad. So this uses lots and lots of strings in memory (I've forgotten how to count past 5):
  string stringvar = "a";
  string word2 = "b";
  stringvar += ",";
  stringvar += word2;

Instead, we can do:
   StringBuilder strbString = new StringBuilder;
   <pseudocode>foreach string word in wordsource.words</p>
      strbString.AppendFormat("{0},", word);
   // now remove the last comma from the stringbuilder
   // and then get the string
   string mystringlist = strbString.ToString();

Where is your word list coming from by the way? Is it some kind of object array so you know the length? If so, then yes, you can miss off the last comma.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.