Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# substring
Message
From
06/05/2013 14:56:12
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01572787
Message ID:
01572797
Views:
86
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;
}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform