stringa.isnululrempty usando trim ()
-
14-12-2019 - |
Domanda
Voglio rimuovere la prima riga:
!string.IsNullOrEmpty(cell.Text)
.
Ciò causa qualsiasi problema?
Ho corso su questo in qualche codice:
if ((id % 2 == 0)
&& !string.IsNullOrEmpty(cell.Text)
&& !string.IsNullOrEmpty(cell.Text.Trim())
)
.
Penso che la prima stringa.isnullullore E la linea con finiture () si prende cura di questo, quindi il primo isnorempty è inutile
Ma prima di rimuovere la linea senza il ritaglio, pensavo di eseguirlo dal gruppo.
Soluzione
Il primo isnorempiant cattura i valori nullo prima di lanciare un nullreferenceexception con finiture ().
Tuttavia, c'è un modo migliore:
if ((id % 2 == 0) && !string.IsNullOrWhiteSpace(cell.Text))
. Altri suggerimenti
Se cell.text è nullo, avresti un'eccezione senza quel primo controllo.
in .NET 4.0:
if (id % 2 == 0 && !string.IsNullOrWhiteSpace(cell.Text))
{
...
}
.
In versioni precedenti dovresti conservare i due test perché se si rimuove il primo e cell.Text
è NULL, riceverai un NRE sul secondo quando si tenta di invocare .Trim
su un'istanza nullo.
O puoi anche fare questo:
if (id % 2 == 0 && string.IsNullOrWhiteSpace((cell.Text ?? string.Empty).Trim()))
{
...
}
.
o ancora meglio, potresti scrivere un Metodo di estensione perIl tipo di stringa che lo farà in modo da poter semplicemente:
if (id % 2 == 0 && !cell.Text.IsNullOrWhiteSpace())
{
...
}
.
che potrebbe sembrare così:
public static class StringExtensions
{
public static bool IsNullOrWhiteSpace(this string value)
{
return string.IsNullOrEmpty((value ?? string.Empty).Trim());
}
}
. Credo che il test sia quello di garantire che la cella.Text non sia null prima ... Se sì, cercando di bypassarlo e ottenere solo cell.text.trim () si soffocherebbe come non puoi fare un ritaglio su astringa null.
Perché non utilizzare !string.IsNullOrWhitespace(call.Text)
e rilasciare i due controlli precedenti?
Non è possibile rimuovere solo il primo isnoremply mentre la cella.Text potrebbe essere nullo e quindi chiamare il tagliente su di esso avrebbe lanciato ed eccezione.Usa isnullorwhithaspace Se stai usando .NET 4.0 o lasciareentrambi controlli.
if ((id % 2 == 0) && !string.IsNullOrWhiteSpace(cell.Text))
. Se cell.text è null, la stringa di espressione.isnulluorempty (cell.text.trim ()) lancerà un'eccezione poiché sta tentando di eseguire il metodo Trim () sulla cella.
Molto più laggibile se la condizione sarebbe: cell.text!= null && cell.text.trim ()!=""
È possibile utilizzare il metodo di estensione come questo:
/// <summary>
/// Indicates whether the specified string is null or empty.
/// This methods internally uses string.IsNullOrEmpty by trimming the string first which string.IsNullOrEmpty doesn't.
/// .NET's default string.IsNullOrEmpty method return false if a string is just having one blank space.
/// For such cases this custom IsNullOrEmptyWithTrim method is useful.
/// </summary>
/// <returns><c>true</c> if the string is null or empty or just having blank spaces;<c>false</c> otherwise.</returns>
public static bool IsNullOrEmptyWithTrim(this string value)
{
bool isEmpty = string.IsNullOrEmpty(value);
if (isEmpty)
{
return true;
}
return value.Trim().Length == 0;
}
.