My aim is to generate a string. which has following conditions.
First i will take an empty string and start building the string based on length.
Example:
I have an empty string "".
In the first step i want to add a string till 8 characters, means first my string is "" then till 8 characters my string should contain the value Hiwor so finally my string will be Hiwor if there is no value empty value should be padded in the string.
In the second step i want to add the string meena till 10 positions , so my final string should be Hiwor meena. In this way i want to build my string. Ho can i achieve this. Can you please help me.
Sample:
initial string ""
first step adding string Hiwor till 8 positions,
so final string should be Hiwor
second step adding string meena till 10 postions
so final string should be Hiwor meena .
Till now i tried like this
Dictionary<string, Int16> transLine = new Dictionary<string, Int16>();
transLine.Add("ProductCode", 1);
transLine.Add("ApplicantFirstName", 12);
transLine.Add("ApplicantMiddleInitial", 1);
transLine.Add("partner", 1);
transLine.Add("employee", 8);
List<string> list = new List<string>();
list.Add("grouplife");
list.Add("meena");
list.Add("d");
list.Add("yes");
list.Add("yes");
StringBuilder sb = new StringBuilder();
foreach (var listItem in list)
{
foreach (var item in transLine)
{
if (listItem == item.Key)
{
var length = item.Value;
sb.Insert(length, item.Key);
}
}
}
but it is throwing me an exception.Index was out of range. Must be non-negative and less than the size of the collection.
Firstly define an extension method for StringBuilder:
public static class StringBuilderExtensions
{
public static void AppendPadded(this StringBuilder builder, string value, int length);
{
builder.Append($"{value}{new string(' ', length)}".Substring(0, length));
}
public static void AppendPadded(this StringBuilder builder, int value, int length);
{
builder.Append($"{new string('0', length)}{value}".Reverse().ToString().Substring(0, length).Reverse().ToString());
}
}
Then use:
StringBuilder builder = new StringBuilder();
builder.AppendPadded("Hiwor", 8);
builder.AppendPadded("meena", 10);
return builder.ToString();
Or with your example:
foreach (string item in list)
builder.AppendPadded(item, transLine[item]);
EDIT: Ok, so looks like you want to be able to define a format then build the string using it. Try:
(you will need to reference System.ComponentModel.DataAnnotations and System.Reflection for this)
public abstract class AnItem
{
private static int GetLength(PropertyInfo property)
{
StringLengthAttribute attribute = property.GetCustomAttributes(typeof(StringLengthAttribute), true).FirstOrDefault() as StringLengthAttribute;
if (attribute == null)
throw new Exception($"StringLength not specified for {property.Name}");
return attribute.MaxLength();
}
private string GetPropertyValue(PropertyInfo property)
{
if (property.PropertyType == typeof(string))
return property.GetValue(this);
else if (property.PropertyType == typeof(int))
return property.GetValue(this).ToString();
else
throw new Exception($"Property '{property.Name}' is not a supported type");
}
private static void SetPropertyValue(object item, PropertyInfo property, string value)
{
if (property.PropertyType == typeof(string))
property.SetValue(item, value, null);
else if (property.PropertyType == typeof(int))
property.SetValue(item, int.Parse(value), null);
else
throw new Exception($"Property '{property.Name}' is not a supported type");
}
public string GetPaddedString()
{
StringBuilder builder = new StringBuilder();
PropertyInfo[] properties = GetType().GetProperties();
foreach (PropertyInfo property in properties)
builder.AppendPadded(GetPropertyValue(property), GetLength(property));
return builder.ToString();
}
public static T CreateFromPaddedString<T>(string paddedString) where T : AnItem, new()
{
T item = new T();
int offset = 0;
PropertyInfo[] properties = typeof(T).GetProperties();
foreach (PropertyInfo property in properties)
{
int length = GetLength(property);
if (offset + length > paddedString.Length)
throw new Exception("The string is too short");
SetPropertyValue(item, property, paddedString.Substring(offset, length)));
offset += length;
}
if (offset < paddedString.Length)
throw new Exception("The string is too long");
return item;
}
}
public class MyItem : AnItem
{
[StringLength(1)]
public string ProductCode { get; set; }
[StringLength(12)]
public string ApplicantFirstName { get; set; }
[StringLength(1)]
public string ApplicantMiddleInitial { get; set; }
[StringLength(1)]
public string Partner { get; set; }
[StringLength(8)]
public string Employee { get; set; }
}
Then use it:
MyItem item = new MyItem
{
ProductCode = "grouplife",
ApplicantFirstName = "meena",
ApplicantMiddleInitial = "d",
Partner = "yes",
Employee = "yes"
};
string paddedString = item.GetPaddedString();
And to read a string to get an item:
MyItem item = AnItem.CreateFromPaddedString<MyItem>(paddedString);
At first I want to say something more about your exception:
Index was out of range. Must be non-negative and less than the size of the collection.
As the exception already said. The problem is that you want to access a position within your new StringBuilder sb which does not exists.
StringBuilder sb = new StringBuilder();
After this line your new sb is empty. There is no single character in it. So you can only access the index at position 0. But almost in your first iteration of the inner for-each loop you want to target the index 1 and try to insert your string at the position 1, which does not exists.
// length: 1 and item.Key: ProductCode
sb.Insert(length, item.Key);
So how to solve this. You can use a feature from String.Format() or since C#6 the string interpolation.
So for example:
String.Format()
var sb = new StringBuilder(string.Empty); // sb: []
sb.Append(string.Format("{0, -8}", "Hiwor")); // sb: [Hiwor ]
sb.Append(string.Format("{0,-10}", "meena")); // sb: [Hiwor meena ]
C#6 String Interpolation
var sb = new StringBuilder(string.Empty); // sb: []
sb.Append($"{"Hiwor", -8}"); // sb: [Hiwor ]
sb.Append($"{"meena", -10}"); // sb: [Hiwor meena ]
// ...
Targeting your edit:
With your given list items you will never get a match with any of your dictionary keys.
Related
I am making a web service for an app with Tesseract Ocr 3.02.
I want to create variables on depending of how many informations I get on the business card and after that classify information from a string.
For example:
Tel. +496123456789$Mobil +49123456789$kai.kalsbach#gmail.com$www.google.com$Kai Kalsbach$Muster Str 1a$40599 Düsseldorf$"
And then like this:
-Telephone number
-First Name
-Last Name
-Email
-Address
That was my first idea:
string endText1 = text.Split('$')[0];
string endText2 = text.Split('$')[1];
string endText3 = text.Split('$')[2];
string endText4 = text.Split('$')[3];
string endText5 = text.Split('$')[4];
string endText6 = text.Split('$')[5];
string endText7 = text.Split('$')[6];
string endText8 = text.Split('$')[7];
and after that i would classify the variables.
but in many cases I get the following exception because the number of informations can vary depending of business card.
System.IndexOutOfRangeException: Index was outside the bounds of the array c#
The IndexOutOfRangeException exception is thrown because the code tries to access an item outside the array length.
My proposition : I created formattedArray with contains always 8 items and I copied the splited array to this formattedArray. With that, you have no more IndexOutOfRangeException because the item missing in text.Split('$') is null in formattedArray
var a = text.Split('$');
var formattedArray = new string[8];
Array.Copy(a, formattedArray, a.Length);
string endText1 = formattedArray [0];
string endText2 = formattedArray [1];
string endText3 = formattedArray [2];
string endText4 = formattedArray [3];
string endText5 = formattedArray [4];
string endText6 = formattedArray [5];
string endText7 = formattedArray [6];
string endText8 = formattedArray [7];
string[] Splitted = text.Split('$');
And you mentioned you want to make a decision based on the number of elements the split spits out
int Count = Splitted.Length;
switch(Count)
{ case 0: //DoStuff
break;
....
default:
break;
}
In your case, it is better to use the following:
string[] stringList = text.Split('$');
foreach(string val in stringList)
{
//your logic.
}
You can split the string once using the .Split method.
Then afterwards run it in a foreach or for loop. I believe your logic is based on the amount of strings, so you are looking for a 'for' loop.
string[] split = text.Split('$');
for (int i = 0; i < split.Length; i++)
{
var text = split[i];
// Your logic here...
switch (i) // for logic based on the index of the string
{
case 0:
// do something
break;
case 1:
// do something
break;
}
}
The IndexOutOfRangeException exception is thrown because the code tries to access the 8th item in a 7-item array :
string endText8 = text.Split('$')[7];
Indexes in .NET collections are 0-based which means 7 refers to the 8th element.
By default, String.Split will return empty fields as well. This means that either the string isn't the same as the one posted here, or that the StringSplitOptions.RemoveEmptyEntries was used
String.Split returns a string array that can be stored in a string[] variable. There's no need to repeat String.Split, or use multiple variables :
var items = text.Split(new[]{'$'},StringSplitOptions.RemoveEmptyEntries);
Creating a class from this array is simple enough that you probably don't need to create a custom parser :
class Record
{
public string Telephone {get;set;}
...
}
var items = text.Split('$');
var record=new Record
{
Telephone=items[0],
Mobile=items[1],
...
};
Another easy way to do that is to use a try, then all variables will be created until the index has reached its maximum.
string[] strArray = text.Split('$');
Try {
string endText1 = strArray[0];
string endText2 = strArray[1];
string endText3 = strArray[2];
string endText4 = strArray[3];
string endText5 = strArray[4];
string endText6 = strArray[5];
string endText7 = strArray[6];
string endText8 = strArray[7];
}
catch
{
//nothing
}
Create factory and recognizers
public class PhoneItem : IItem
{
public PhoneItem(string text)
{
// some code
}
}
public interface IRecognizer
{
IItem Recognize(int index, string text);
}
public class PhoneRecognizer : IRecognizer
{
public IItem Recognize(int index, string text)
{
return index == 0 ? new PhoneItem(text) : null;
}
}
public class ItemFactory
{
private IEnumerable<IRecognizer> _recognizers = new []
{
new PhoneRecognizer(),
new FullNameRecognizer()
};
public IItem CreateItem(int index, string text)
{
foreach (var rec in _recognizers)
{
var item = rec.Recognize(index, text);
if (item != null)
{
return item;
}
}
throw new Exception("Item not recognized");
}
}
Split string to pieces
var parts = text.Split('$');
Use the factory to create objects
var factory = new ItemFactory();
var items = new List<IItem>();
for (int i = 0; i < parts.Length; i++)
{
items.Add(factory.CreateItem(i, parts[i]));
}
// do whatever you wants
I have a list which stores some string values.
Code:
List<VBCode> vbCodes = new List<VBCode>();
public class VBCode
{
public string Formula { get; set; }
}
In a method I am trying to append the list values.
public void ListValue()
{
if (vbCodes.Count > 0)
{
StringBuilder strBuilder = new StringBuilder();
foreach (var item in vbCodes)
{
strBuilder.Append(item).Append(" || ");
}
string strFuntionResult = strBuilder.ToString();
}
}
The list will have values like shown below
How can I get the formula values and append in the foreach?
You are appending the item object you need to append the object property Formula
public void ListValue()
{
if (vbCodes.Count > 0)
{
StringBuilder strBuilder = new StringBuilder();
foreach (var item in vbCodes)
{
strBuilder.Append(item.Formula).Append(" || ");
}
string strFuntionResult = strBuilder.ToString();
}
}
You can do this simply without foreach by using String.Join(), it will be like this :
string strFuntionResult = String.Join(" || ", vbCodes.Select(x=>x.Formula).ToList());
If you really want to iterate using foreach means you have to get Formula from the iterator variable also take care to remove the final || after completing the iteration, if so the code would be like the following:
StringBuilder strBuilder = new StringBuilder();
foreach (var item in vbCodes)
{
strBuilder.Append(item.Formula).Append(" || ");
}
string strFuntionResult = strBuilder.ToString(); // extra || will be at the end
// To remove that you have to Trim those characters
// or take substring till that
strFuntionResult = strFuntionResult.Substring(0, strFuntionResult.LastIndexOf('|'));
I currently have a dictionary(string,int) which will hold values like the following
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[1] , 91
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[2] , 92
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[2]/FormA1[1] , 93
And this collection is created using a simple method CreatePathCollection(string path, int entityKey)
However the challenge I am facing is the following.
Suppose I receive a key and value into my method, which has values like
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[1] , 94
I would like to update the the following keys in the collection from
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[1] , 91
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[2] , 92
TO
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[2] , 91
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[3] , 92
And then Add
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[1] , 94
So the final collection will be
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[1] , 94
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[2] , 91
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[3] , 92
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[2]/FormA1[1] , 93
Is there an elegant way to accomplish this?
Create a model to represent your key like this:
The following class represents part of the path like /ReturnState[1], and it contains a method (the constructor) to parse the data from a string and another method to convert the data to the string format.
public class Part
{
public string Name { get; set; }
public int Index { get; set; }
public Part(string str)
{
int location_of_bracket_start = str.LastIndexOf("[");
if(location_of_bracket_start == -1)
throw new Exception("Unexpected format");
Name = str.Substring(0, location_of_bracket_start);
string rest = str.Substring(location_of_bracket_start);
Index = int.Parse(rest.Substring(1, rest.Length - 2));
}
public string ConvertToStringFormat()
{
return string.Format("/{0}[{1}]", Name, Index);
}
}
The following class represents a full path (e.g. /ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[1]) as a list of parts. It also contains method to construct an the object from a string and to convert to a string.
public class NodePath : List<Part>
{
public NodePath(string path)
{
string[] parts = path.Split(new []{"/"}, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in parts)
{
this.Add(new Part(part));
}
}
public string ConvertToStringFormat()
{
return string.Join("", this.Select(x => x.ConvertToStringFormat()));
}
}
The following class contains the logic that you need:
public class PathClass
{
private readonly Dictionary<string, int> m_Dictionary;
public PathClass()
{
m_Dictionary = new Dictionary<string, int>();
}
public Dictionary<string, int> Dictionary
{
get { return m_Dictionary; }
}
public void Add(string path, int number)
{
if (m_Dictionary.ContainsKey(path))
MoveOne(path);
m_Dictionary.Add(path, number);
}
public void MoveOne(string path)
{
int number = m_Dictionary[path];
m_Dictionary.Remove(path);
var moved_node_path = IncrementPath(path);
if (m_Dictionary.ContainsKey(moved_node_path))
MoveOne(moved_node_path);
m_Dictionary.Add(moved_node_path, number);
}
private string IncrementPath(string path)
{
NodePath node_path = new NodePath(path);
node_path.Last().Index++;
return node_path.ConvertToStringFormat();
}
}
When the consumer tries to add a path, it checks if it exists, if it does, it moves the existing one (increments the index of the last path Part). It does this recursively in case the dictionary also contains an item where we are trying to move to.
I tested this like this:
PathClass path_class = new PathClass();
path_class.Add("/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[1]" , 1);
path_class.Add("/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[1]", 2);
path_class.Add("/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[2]/FormA1[1]", 3);
path_class.Add("/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[2]/FormA1[2]", 4);
path_class.Add("/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[2]/FormA1[1]", 5);
I got the following results:
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[2], 1
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1[1], 2
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[2]/FormA1[2], 3
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[2]/FormA1[3], 4
/ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[2]/FormA1[1], 5
Please note that another way to do this is to use a Dictionary<NodePath,int>, this means that you would need to implement Equals and GetHashCode for NodePath.
UPDATE:
If you don't care about the model, you can replace the IncrementPath method with this (and remove the model) for performance reasons:
private string IncrementPath(string path)
{
int location_of_bracket_start = path.LastIndexOf("[");
if (location_of_bracket_start == -1)
throw new Exception("Unexpected format");
string before_bracket = path.Substring(0, location_of_bracket_start);
string rest = path.Substring(location_of_bracket_start);
int index = int.Parse(rest.Substring(1, rest.Length - 2));
index ++;
return string.Format("{0}[{1}]", before_bracket, index);
}
Here is what I end up - not very elegant, but should do the job
static void UpdatePathCollection(Dictionary<string, int> target, string path, int entityKey)
{
int start, index;
if (path == null || path.Length < 3 || path[path.Length - 1] != ']'
|| (start = path.LastIndexOf('[', path.Length - 2)) < 0
|| !int.TryParse(path.Substring(start + 1, path.Length - start - 2), out index)
|| index < 0) throw new ArgumentException("path");
var prefix = path.Substring(0, start + 1);
var nextKey = path;
var nextValue = entityKey;
while (true)
{
int oldValue;
if (!target.TryGetValue(nextKey, out oldValue))
{
target.Add(nextKey, nextValue);
break;
}
target[nextKey] = nextValue;
index++;
nextKey = prefix + index + "]";
nextValue = oldValue;
}
}
As far as I understand, the strings you use to define the paths will occur in alphabetical order (it will depend on whether you have more than 9 elements per index). In this scenario, you might use SortedDictionary and proceed as follows:
private readonly SortedDictionary<string, int> sortedDictionary = CreatePathCollection(path, entityKey);
public void Set(string path, int index)
{
sortedDictionary.Remove(path);
var i = 91;
foreach (var key in sortedDictionary.Keys)
sortedDictionary[key] = i++;
sortedDictionary[path] = index;
}
Unfortunately, I may not understand your problem in all detail, but I hope this gives you some ideas.
I am trying to display my results as follows:
-.-|- [tab] kt
-.|-- [tab] nm
-.|-|- [tab] ntt
But this is my current output
-.-|-| kt
-.|--| nm
-.|-|-| [tab]ntt
There is a | at the end of every Morse code which I would like to remove since it is at the end.
Also because the user can input Morse code with space between dots and dashes - i noticed that it affects the alignment of the characters and not all of them get tabbed properly. The word tab isn't supposed to show i just wrote it in because I didn't know how to place a real tab.
private static readonly IDictionary<char, string> morseCode_alpha = new Dictionary<char, string>
{
{'a', ".-"},{'b',"-..."}, {'c',"-.-."}, {'d',"-.."}, {'e',"."},
{'f',"..-."}, {'g',"--."}, {'h',"...."},{'i',".."}, {'j',".---"},
{'k',"-.-"}, {'l',".-.."}, {'m',"--"}, {'n',"-."}, {'o',"---"},
{'p',".--."}, {'q',"--.-"}, {'r',".-."}, {'s',"..."}, {'t',"-"},
{'u',"..-"}, {'v',"...-"}, {'w',".--"}, {'x',"-..-"}, {'y',"-.--"}, {'z',"--.."}
};
private static string ConvertMorseToText(string symbolCode)
{
var builder = new StringBuilder(4 * symbolCode.Length);
foreach (char c in symbolCode)
builder.Append(morseCode_alpha[c]);
return builder.ToString();
}
private static string ConvertTextToMorse(char ch)
{
if (morseCode_alpha.Keys.Contains(ch))
return morseCode_alpha[ch];
else
return string.Empty;
}
private static string ConvertStringToMorse(string letters)
{
StringBuilder sb = new StringBuilder();
foreach (char ch in letters)
{
if (sb.Length != 0 && sb[sb.Length - 1] != ' ')
sb.Append("|");
sb.Append(ConvertTextToMorse(ch));
}
return sb.ToString();
}
private static IEnumerable<string> Permutations( string symbolCode)
{
int n = symbolCode.Length;
if (n == 0 || symbolCode.Length == 0)
yield return " ";
else
foreach (var entry in morseCode_alpha)
if (symbolCode.StartsWith(entry.Value))
foreach (string next in Permutations(symbolCode.Substring(entry.Value.Length)))
yield return entry.Key + next;
}
private static void Write( string rest)
{
string result = ConvertStringToMorse(rest);
Console.Write(result+"\t");
Console.WriteLine(rest);
}
static void Main(string[] args)
{
string morseInput;
string entered = "";
do
{
Console.WriteLine("Enter Morse Code: \n");
morseInput = Console.ReadLine().Replace(" ","");
bool isValid = Regex.IsMatch(morseInput, #"^[-.]+$");
if (isValid)
{
Console.WriteLine("\nAll permutations:\n");
string morse = ConvertMorseToText(entered);
string permutations = morseInput.Substring(morse.Length);
Write(permutations);
var nexts = new List<string>(Permutations(permutations));
foreach (string next in nexts)
Write(next);
}
else
{
Console.WriteLine("\nFormat of morse must be only dots and dashes.");
Console.WriteLine("Parameter name: "+morseInput+"\n");
}
}
while (morseInput.Length != 0);
}
And, to answer the other part of the question...
Tabstops are fixed for console writing, so it would be better to use something like String.PadRight
so, your code could be:
private static void Write(string rest)
{
string result = ConvertStringToMorse(rest);
Console.Write(result.PadRight(20));
Console.WriteLine(rest);
}
Draft version of the method:
private static string ConvertStringToMorse(string letters)
{
var result = string.Join("|",
letters
.Select(ConvertTextToMorse)
.Where(morse => !string.IsNullOrEmpty(morse)));
return result;
}
Update:
Please note that the entered variable is used only once: when defined - empty string is assigned. Then the ConvertMorseToText(entered) method is called: it always returns empty string for the empty string argument. After this assignment string permutations = morseInput.Substring(morse.Length); the permutations variable will store exactly the same value as morse variable (because morse.Length is always 0).
So, it seems that the entered variable and the ConvertMorseToText() method are useless (both can be safely removed):
static void Main(string[] args)
{
do
{
Console.WriteLine("Enter Morse Code: ");
string morseInput = Console.ReadLine();
if (string.IsNullOrWhiteSpace(morseInput))
{
// Empty or consists only of white-space characters
break;
}
morseInput = morseInput.Replace(" ", "");
bool isValid = Regex.IsMatch(morseInput, #"^[-.]+$");
if (isValid)
{
Console.WriteLine("All permutations:");
Console.WriteLine();
var nexts = Permutations(morseInput).ToList();
foreach (string next in nexts)
Write(next);
}
else
{
Console.WriteLine();
Console.WriteLine("Format of morse must be only dots and dashes.");
Console.WriteLine("Parameter name: {0}", morseInput);
}
}
while (true);
}
Update 2:
Consider using TryGetValue() method of Dictionary<TKey, TValue> instead of Keys.Contains and [] (indexer) i.e. do not perform look-up twice:
private static string ConvertTextToMorse(char ch)
{
string result;
return morseCode_alpha.TryGetValue(ch, out result) ? result : string.Empty;
}
Instead this code:
Console.Write(result+"\t");
Console.WriteLine(rest);
Use
Console.WriteLine("{0,-10}{1,-10}", result, rest);
Then you will see two columns (each max 10 charachters) with left alignment. Or remove "-" sign if you want right alignment.
Building a string for post request in the following way,
var itemsToAdd = sl.SelProds.ToList();
if (sl.SelProds.Count() != 0)
{
foreach (var item in itemsToAdd)
{
paramstr = paramstr + string.Format("productID={0}&", item.prodID.ToString());
}
}
after I get resulting paramstr, I need to delete last character & in it
How to delete last character in a string using C#?
Personally I would go with Rob's suggestion, but if you want to remove one (or more) specific trailing character(s) you can use TrimEnd. E.g.
paramstr = paramstr.TrimEnd('&');
build it with string.Join instead:
var parameters = sl.SelProds.Select(x=>"productID="+x.prodID).ToArray();
paramstr = string.Join("&", parameters);
string.Join takes a seperator ("&") and and array of strings (parameters), and inserts the seperator between each element of the array.
string source;
// source gets initialized
string dest;
if (source.Length > 0)
{
dest = source.Substring(0, source.Length - 1);
}
Try this:
paramstr.Remove((paramstr.Length-1),1);
I would just not add it in the first place:
var sb = new StringBuilder();
bool first = true;
foreach (var foo in items) {
if (first)
first = false;
else
sb.Append('&');
// for example:
var escapedValue = System.Web.HttpUtility.UrlEncode(foo);
sb.Append(key).Append('=').Append(escapedValue);
}
var s = sb.ToString();
string str="This is test string.";
str=str.Remove(str.Length-1);
It's better if you use string.Join.
class Product
{
public int ProductID { get; set; }
}
static void Main(string[] args)
{
List<Product> products = new List<Product>()
{
new Product { ProductID = 1 },
new Product { ProductID = 2 },
new Product { ProductID = 3 }
};
string theURL = string.Join("&", products.Select(p => string.Format("productID={0}", p.ProductID)));
Console.WriteLine(theURL);
}
It's good practice to use a StringBuilder when concatenating a lot of strings and you can then use the Remove method to get rid of the final character.
StringBuilder paramBuilder = new StringBuilder();
foreach (var item in itemsToAdd)
{
paramBuilder.AppendFormat(("productID={0}&", item.prodID.ToString());
}
if (paramBuilder.Length > 1)
paramBuilder.Remove(paramBuilder.Length-1, 1);
string s = paramBuilder.ToString();
paramstr.Remove((paramstr.Length-1),1);
This does work to remove a single character from the end of a string. But if I use it to remove, say, 4 characters, this doesn't work:
paramstr.Remove((paramstr.Length-4),1);
As an alternative, I have used this approach instead:
DateFrom = DateFrom.Substring(0, DateFrom.Length-4);
Add a StringBuilder extension method.
public static StringBuilder RemoveLast(this StringBuilder sb, string value)
{
if(sb.Length < 1) return sb;
sb.Remove(sb.ToString().LastIndexOf(value), value.Length);
return sb;
}
then use:
yourStringBuilder.RemoveLast(",");