Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# substring
Message
 
 
To
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:
01572806
Views:
36
>>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;
>}
>
Thanks a lot, this is going to work great.
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform