Click here to Skip to main content
14,774,351 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi!

My Strings look like this:
Hello; World;

The Attribute of my class is DelimitedRecord(";")

Now i get 2 Fields back.

Hello
World;

How i can cut the ";" from the last word.

Best Regards Alex!
Posted
Updated 26-May-11 9:51am
v2

You can call string.TrimEnd(';').

string mystring = "Hello;";

string mystring = mystring.TrimEnd(';');

// Output is "Hello"
Console.WriteLine(mystring);
   
Comments
Sergey Alexandrovich Kryukov 26-May-11 14:10pm
   
As simple as that, a 5.
--SA
Kim Togo 27-May-11 8:11am
   
Thanks SA
fcronin 26-May-11 15:32pm
   
Is good, 5... just made another note below, use it in the class so the consumer doesn't need to sweat it.
Kim Togo 27-May-11 8:12am
   
Thanks fcronin
You can do like this also,
string s= "World;";

s = s.Remove(s.Length-1);
   
Well, if you wrote the class and the attibute DelimitedRecord(string)... if I were you and you do not want to return any empty split results... I would change your method in the class so you don't have to handle that every time.

public string DelimitedRecord(char delimiterChar)
{
   return mystring.TrimEnd(delimiterChar).Split(delimiterChar);
}
   
v2
Comments
Kim Togo 27-May-11 8:19am
   
Yes you are right about changing the method for DelimitedRecord.
But the code you provide does not compile.
fcronin 27-May-11 9:52am
   
My apologies, I was just speeding thru it... it should return a string[] rather than a string.

<pre>
public string[] DelimitedRecord(char delimiterChar)
{
return mystring.TrimEnd(delimiterChar).Split(delimiterChar);
}
</pre>
If you can change the way how DelimitedRecord works. Then try to change it to

public string[] DelimitedRecord(char delimiterChar)
{
  foreach (string record in myOrginalString.Split(delimiterChar))
  {
    // To some other works on each string from .Split
    if (record == "ID1123")
    {
      record = "CURRENT " + record;
    }

    yield return record;
  }
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900