3

There is an array of characters e.g.

char [] biggerText = new char [8];

then I input some values likewise:

biggerText[0] = (char) 12;
biggerText[1] = (char) 48;

then I use the following operation:

string smallerText= string.Join("", biggerText);

and when you perform smallerText.Length you get 8 and NOT 2.

What is the most effective method to trim this array to omit the null values when converting to string?

1
  • did you tried some answer? Did they work? Do you need more help? Commented Jun 1, 2017 at 16:58

7 Answers 7

3

Why don't you use a List<char>? This way, you don't have to worry about indexes, and you don't have to know the final length at the beginning (as pointed out in the comment by @n8wrl).

var list = new List<char>();

list.Add((char)12);
list.Add((char)48);

Then you can get the final string in a more elegant way:

var smallerText = new string(list.ToArray());

And this string will have the correct Length.

Sign up to request clarification or add additional context in comments.

1 Comment

AND you don't have to declare the size up-front! A huge help in two years when your code changes and you're working with 9 chars (or more!)
2

You can make a new string from the array and trim the null chars from the end:

string smallerText = new String(biggerText).TrimEnd('\0');

Comments

1

This should do the trick.

string smallerText = string.Join("", biggerText.Where(n => n != '\0'));

This is a good way to do it, because Trim will work just with white spaces and may not work well for you.

3 Comments

"Trim will work just with white spaces" - not so. You should review the documentation for Trim.
I did. Removes all leading and trailing white-space characters from the current String object.
There is also the String.Trim Method (Char()) overload which takes an array of user-defined characters to trim. Strangely, there is no documentation which says that you can supply it with a char instead an array of char, but it works.
1

You can use TakeWhile to only take the characters until you see a "null" character assuming you don't want any of the characters that might exist after the first "null" character.

string smallerText= string.Join("", biggerText.TakeWhile(c => c != '\0'));

Comments

0
char [] biggerText = new char [8];

    biggerText[0] = (char)12;
    biggerText[1] = (char)48;

    string smallerText= new string(biggerText).Replace("\0", string.Empty);
    int length = smallerText.Length;

\0 can be at any location, so just replace it with empty value.

Comments

0

If you mean the most efficient:

string smallerText = new string(biggerText, 0, 2);

but "the most effective method to trim" is the .Trim method 😊

string smallerText = new string(biggerText).TrimEnd(char.MinValue);

Comments

0

Here's my solution with spans!

ReadOnlySpan<char> chars = biggerText;
string smallerText = chars.TrimEnd(char.MinValue).ToString();

In my opinion, this is much better than other answers. It wraps char array as ReadOnlySpan<char> and trim it's end by calculating actual length.

Note that it's working better with arrays, not strings. For strings, use string.TrimEnd(char);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.