Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# substring
Message
De
06/05/2013 14:56:12
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01572787
Message ID:
01572797
Vues:
85
This message has been marked as the solution to the initial question of the thread.
>Hi everybody,
>
>I have this method
>
>
>internal void GetDCIComponents(String tcDCI, out String department, out String category, out String item)
>      {
>         department = tcDCI.Substring(0, 10);
>         category = tcDCI.Substring(10, 10);
>         item = tcDCI.Substring(20, 10);
>      }
>
>The problem is with the last assignment when I pass item less than 10 characters. What is the best way to "simulate" VFP substring to get up to the length of the item?
>
>Thanks in advance.

Try this as an extension method:
public static string SafeSubstring(this string originalValue, int startIndex, int length)
{
	string retVal = string.Empty;

	if (!string.IsNullOrWhiteSpace(originalValue))
	{
		if (startIndex >= originalValue.Length)
			startIndex = originalValue.Length - 1;

		if (startIndex < 0)
			startIndex = 0;

		if (startIndex + length > originalValue.Length)
			length = originalValue.Length - startIndex;

		if (0 <= startIndex && startIndex < originalValue.Length && length > 0)
			retVal = originalValue.Substring(startIndex, length);
	}
	
	return retVal;
}
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform