I am retrieving a number of records from a database, which I append to some string - for example:
spouse counter=1;
counter=1+counter;
then I get customerID in the following way:
customerID = "AGP-00000" + counter;
so that customerID will be AGP-000001.
For counter value 2 - the customerID string will be AGP-000002.
My problem is that, if counter is 10 then customer ID will be AGP-0000010, and I want it to be AGP-000010.
How can I ensure that customer ID is always like AGP-000010?
int number = 1;
string customerID = "AGP-" + number.ToString("D6");
customerID will be: "AGP-000001"
See: How to: Pad a Number with Leading Zeros
Not sure if it's the most efficient way, but it works
string str = "AGP-00000";
int counter = 100;
str = str.Substring(0, str.Length - counter.ToString().Length + 1);
str += counter.ToString();
Related
I am new to C#. My problem is to take odd chars from a string and get a new string from those odds.
string name = "Filip"; // expected output ="Flp"
I don't want to take, for example,
string result = name.Substring(0, 1) + name.Substring(2, 1) + ... etc.
I need a function for this operation.
Try Linq (you actually want even characters since string is zero-based):
string name = "Filip";
string result = string.Concat(name.Where((c, i) => i % 2 == 0));
In case of good old loop implementation, I suggest building the string with a help of StringBuilder:
StringBuilder sb = new StringBuilder(name.Length / 2 + 1);
for (int i = 0; i < name.Length; i += 2)
sb.Append(name[i]);
string result = sb.ToString();
how can i get string between two dots for example ?
[Person.Position.Name]
for this case I want to get the string "Position"
I can also have three dots ….
[Person.Location.City.Name]
I want to take all strings between dots
I know it's a year old question, but the other answers are not sufficient, like they are even pretending that you want "Location.City" because they don't know how to seperate them.. The solution is simple though, don't use indexof.
say you want to seperate the Four (not 3) parts:
String input = "Person.Location.City.Name"
string person = input.Split('.')[0];
string location = input.Split('.')[1];
string city = input.Split('.')[2];
string name = input.Split('.')[3];
Console.WriteLine("Person: " + person + "\nLocation: " + location + "\nCity: " + city + "\nName: " + name);
This might help you:
string s = "Person.Position.Name";
int start = s.IndexOf(".") + 1;
int end = s.LastIndexOf(".");
string result = s.Substring(start, end - start);
It will return all the values between the first and the last dot.
If you don't want the result with dot between the strings, you can try this:
string s = "Person.Location.Name";
int start = s.IndexOf(".") + 1;
int end = s.LastIndexOf(".");
var result = s.Substring(start, end - start).Split('.');
foreach (var item in result)
{
//item is some string between the first and the last dot.
//in this case "Location"
}
Try this
string str = "[Person.Location.City.Name]";
int dotFirstIndex = str.IndexOf('.');
int dotLastIndex = str.LastIndexOf('.');
string result = str.Substring((dotFirstIndex + 1), (dotLastIndex - dotFirstIndex) - 1); // output Location.City
I have a listbox which i am populating with data; a string of variable length and 3 ints formatted to fixed lengths.
I can't work out how to get the string text to take up only x characters
i.e.
30 characters worth of space, if string is 5 characters long add 25 characters worth of padding. If string is 10 characters long add 20 characters of padding.
My latest attempt looked like:
int padding = 30 - item.ProductName.Length;
string prodName = String.Format("{0, " + padding + "}",item.ProductName);
string quant = String.Format("{0,15}", item.GetQuantity);
string price = String.Format("{0,30:C2}", item.LatestPrice);
string total = String.Format("{0,30:C2}", item.TotalOrder);
string temp = prodName + quant + price + total;
return temp;
But that's still not working: http://i.imgur.com/RfxFCO3.png
This should do the trick.
//int padding = 30 - item.ProductName.Length;
string prodName = String.Format("{0, -30}", item.ProductName);
string quant = String.Format("{0,15}", item.GetQuantity);
string price = String.Format("{0,30:C2}", item.LatestPrice);
string total = String.Format("{0,30:C2}", item.TotalOrder);
string temp = prodName + quant + price + total;
return temp;
And if you want Product Names absolutely limited to 30 characters then you'll need to truncate Product Names > 30 characters.
string prodName = String.Format("{0, -30}", item.ProductName.Length > 30 ? item.ProductName.Substring(0,30): item.ProductName);
Is there any way available to give start and ending value to the string and string copy all that values in c#
e.g
My name is testing.
Now i want to copy 'name is'
from the string how i can achieve. I don't have any specific length of the string, It could be increase and decrease.
Try String.IndexOf and String.Substring.
String s1 = "My name is testing.";
String sub = "name is";
int index = s1.IndexOf(sub);
String found = s2.Substring(index, sub.Length);
Well, I'm not completely sure what you are asking here, but...
I don't have any specific length of the string
Sure you do.
string s = "name is";
int len = s.Length; // len == 7
To concatenate strings you can use the + operator.
string prefix = "prefix : "
string suffix = "suffix : "
string s = prefix + "name is" + suffix;
int len = s.Length; // len == 25
I think I nailed your requirement and the solution. Do let me know if this is what you wanted and if this works!
MessageBox.Show(FindStringBetween("My name is farhan.", "My", "is"));
public string FindStringBetween(string SourceString, string StartString, string EndString)
{
int StartSelection = StartString.Length;
int EndSelection = SourceString.IndexOf(EndString)+EndString.Length;
return (SourceString.Substring(StartSelection).Substring(0, EndSelection-StartSelection));
}
I want to insert a space every 34 characters in a string
public string MySplit()
{
string SplitThis = "aaaaaaaaaaaa"; // assume that string has more than 34 chars
string[] array = new string[SplitThis .Length / 34];
for (int i = 1; i <= array.Length; i++)
{
SplitThis .Insert(i * 34, " ");
}
return SplitThis;
}
when I quickwatch "SplitThis .Insert(i * 34, " ");" I can see the space but the resultant string do not show the space. Why?
You are throwing away the result of the insert
Try
SplitThis = SplitThis.Insert(i*34, " ");
But there might be other logic errors in your code because you are amending the same string as you are working one and have calculated the number of iterations based on the length of the string, which is ignoring the fact that the length of the string is changing.