|
HI
I want to find the character position in a word.
for example,If the word contain 8 charcter, i want to print the 5th character in that word.
Thanx in advance.
|
|
|
|
|
string hello = "Hello";
char c = hello[4];
Kristian Sixhoej
"You can always become better." - Tiger Woods
modified on Monday, February 16, 2009 9:00 AM
|
|
|
|
|
IndexOutOfRangeException
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:10 AM
|
|
|
|
|
Uhm.. Is it zero-based?
EDIT: Obviously, it is. I must have been sleeping when I posted that code.
Kristian Sixhoej
"You can always become better." - Tiger Woods
|
|
|
|
|
|
have you solved your problem with the other posts?
komathyc wrote: If the word contain 8 charcter, i want to print the 5th character
Is there a requirement for you to get a character based on the length of the word?
Of course i you had posted a thanks
If only MySelf.Visible was more than just a getter...
A person can produce over 5 times there own body weight in excrement each year... please re-read your questions before posting
|
|
|
|
|
Hello
How we can add two zéro after a comma, for exemple the number 344,5 that transforme on 344,50, and the nmber 333 to 333,00, Thank you verry mutch.
|
|
|
|
|
Console.WriteLine("{0:n2}", 17000)
Would output: 17.000,00
Kristian Sixhoej
"You can always become better." - Tiger Woods
|
|
|
|
|
try this...
string number = "344";//an example
if(number.Contains(','))//already has comma
{
string[] split = number.Split(',', StringSplitOptions.RemoveEmptyValues);
//not 100% sure on the string split option code
if(split.Length == 1)//nothing after the comma
number += "00";
else if(split[1].Length == 1)//need to add 1 zero
number += "0";
//else the number is already as desired
}
else//no comma
number += ",00";
If only MySelf.Visible was more than just a getter...
A person can produce over 5 times there own body weight in excrement each year... please re-read your questions before posting
|
|
|
|
|
I'm sorry to tell you, but this is an extremely bad idea. Basically, if this application ran in a culture on my machine, then it wouldn't work because we use the decimal as the separator, rather than commas. Commas are used here to denote thousands, millions, etc. For instance, we would write 3,442,167.00 which would be written out as garbage by your code.
What you should do, of course, is use a string formatter and actually apply the appropriate culture. That's it - nothing complicated.
|
|
|
|
|
well i dont recall reading anything about different cultures being an issue, for all i know its a simple homework assignment that is to show the logic behind the simple process in question.
Personally i would most likely use the string format method if i needed that kind of functionality, but as i know i can always count on others to provide the one liners, i like to give a bit of an idea to how the process could be done (that way people may learn to learn).
what if at some point the requirement was to put in different chars (for whatever reason) that are not supported by the formatting - yes you could format it then do a replace on the required chars, but then what if they what different chars for the different seperators i.e. thousands = ',' millions = '~' etc.
Well then they would just be back in the forums asking how its done, atleast giving an example of how this is done they may stand a chance to figure things out for themselves if need be, which surely is a step in the right direction for any programmer, no?
If only MySelf.Visible was more than just a getter...
A person can produce over 5 times there own body weight in excrement each year... please re-read your questions before posting
|
|
|
|
|
|
Tony_P wrote: It's actually amazing to see that people still do stuff like that nowadays
where have you seen people using code like i suggested?
If only MySelf.Visible was more than just a getter...
A person can produce over 5 times there own body weight in excrement each year... please re-read your questions before posting
|
|
|
|
|
There's a one-liner for that too.
number+=number.Contains(',')?new String('0',number.indexOf(',')-number.Length+3):",00";
Despite everything, the person most likely to be fooling you next is yourself.
modified on Monday, February 16, 2009 4:58 PM
|
|
|
|
|
very nice
If only MySelf.Visible was more than just a getter...
A person can produce over 5 times there own body weight in excrement each year... please re-read your questions before posting
|
|
|
|
|
for example :
double dbl = 344.5;
string dblInStr = dbl.ToString("F2"); // 344,50
double dbl = 333;
string dblInStr = dbl.ToString("F2"); // 333,00
|
|
|
|
|
Hi Guys,
I'd like to dynamically assign values to a known class instance based on the Property name. An example describes perfectly what I want to do: There is a class
public class AClass
{
public string _aValue;
public string AValue
{
get { return _aValue; }
set { _aValue = value; }
}
}
and I want to assing value to AClass.AValue without actually explicitly writing:
AClass aInstance = new AClass();
aInstance.AValue = "value";
and now comes the best part. I'd like to avoid reflection as much as possible, because performance matters. I could go and look for a property named "AValue" and do
typeof(AClass).GetProperty("AValue").SetValue(aInstance, "value", null);
however that's veery slow. It's got to be possible to do that some other way, Linq works like this and it's not slow.
any ideas?
zilo
|
|
|
|
|
Could try creating a method in the class that takes param name and value, then do a switch and assign the value to the property required
If only MySelf.Visible was more than just a getter...
A person can produce over 5 times there own body weight in excrement each year... please re-read your questions before posting
|
|
|
|
|
thanks for the idea, for now something like that is my workaround solution.
However I wanted to avoid writing special code into the AClass itself as well. I was thinking of something generic that could either AClass inherit from or a Wrapper class.
zilo
|
|
|
|
|
If it's only the one property you could use an implicit operator overload.
public class AClass
{
public AClass() : this(string.Empty) { }
private AClass(string aValue)
{
AValue = aValue;
}
public static implicit operator AClass(string aValue)
{
return new AClass(aValue);
}
public string AValue
{
get;
set;
}
}
AClass aInstance = "Test String";
Console.WriteLine(aInstance.AValue);
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Interesting idea, but... It's not only one, there will be many properties like this within AClass. And it needs to be done outside of constructor.
zilo
|
|
|
|
|
Maybe extension methods could work. Create an extension method ToAClass for each type, and also pass the AClass instance.
public static class ExtensionMethods
{
static public void SetAClass(this string value, AClass instance)
{
instance.AString = value;
}
static public void SetAClass(this int value, AClass instance)
{
instance.AInt = value;
}
}
public class AClass
{
public string AString
{
get;
set;
}
public int AInt
{
get;
set;
}
}
AClass aInstance = new AClass();
"Test string".SetAClass(aInstance);
123.SetAClass(aInstance);
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Anyhow, whether is it extension methods or not, I'll have to create them dynamically using reflection and then just call those methods. I think I have a better idea now, thanks
zilo
|
|
|
|
|
Zilo(svk) wrote: and I want to assing value to AClass.AValue without actually explicitly writing
It's difficult to answer without knowing why. Is it because you don't know the type at compile time?
|
|
|
|
|
Yes. I'm loading the class from dll as
Assembly.LoadFile(string file);
And I'd like to keep AClass as simple as possible, without any specific code to read or write values into it. It's just a data container.
zilo
|
|
|
|