-2

I need to know how to shorten a string to a certain length, I tried string.Trim() but it is not working. Sorry I'm still a beginner.

Thanks

Edit

I'm trying to cut off padding from decrypted text. For example: A user inputs text, the user encrypts and decrypts text. On the decrypted text there is padding left...

Edit 2

I'm using PaddingMode.PKCS7

Dilan V
  • 313
  • 1
  • 3
  • 13

3 Answers3

1

In general the problem is probably in the encryption/decryption. You are encrypting/decrypting with PaddingMode.Zeros, that pads with \0... Now, you could

string decrypted = ...
decrypted = decrypted.TrimEnd('\0');

but it would be at least partially wrong.

Change both the encryption and the decryption adding:

algo.Padding = PaddingMode.ISO10126

(where algo is the object you use to encrypt/decrypt, like a RijndaelManaged)

xanatos
  • 109,618
  • 12
  • 197
  • 280
0

You can use Substring() method of string class like

textBox1.Text.Substring(0, 7);
Rahul
  • 76,197
  • 13
  • 71
  • 125
-1

Strings are immutable, that means you cannot modify them. You have to re-assign the return value of the string methods to the string variable. You can use String.Substring:

int length = textBox1.Text.Length;
longString = longString.Substring(0, Math.Min(length, longString.Length));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939