Position in a string C# [closed] - c#

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I need to know how I check the character in first position in a string on C# code. For example , if the first character is the character "&" or other.
Thanks.

As you can see from the answers, there are many ways to accomplish this. You should be careful to avoid exceptions that will be thrown if you attempt to call methods on a string that is null or use indexers on a string that is null or empty.
if(!String.IsNullOrEmpty(input) && input[0] == '&')
{
// yes
}
or…
if(input != null && input.StartsWith("&"))
{
// yes
}

The easiest way without needing multiple checks is to use the String.CompareOrdinal overload.
string test = "&string";
if (String.CompareOrdinal(test, 0, "&", 0, 1) == 0) {
// String test started with &
}
This has the added benefit of not needing to check for null or empty as the static method handles those automatically.

string test = "&myString";
if(!string.IsNullOrEmpty(test) && test[0] == '&')
{
// first character is &
}

Try using the String.StartsWith method.
if (MyString.StartsWith("&")) {
// do something.
}

Related

How Can Get Characters Between 2 Character? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
How can i get characters between two character ? Example ;
string example = "aaaaaaaaXbbbbXaaaaaa";
How can get bbbb?
You can use String.IndexOf and String.LastIndexOf methods of String class for getting the positions of X's in your string, after that you can use String.SubString method for based their positions.
string example = "aaaaaaaaXbbbbXaaaaaa";
int firstXposition = example.IndexOf("X");
int LastXposition = example.LastIndexOf("X");
Console.WriteLine(example.Substring(firstXposition + 1, LastXposition - firstXposition -1));
Output will be;
bbbb
Here is a DEMO.
You may try like this
where substring holds two parameter
first the starting point of the string after escaping the no of characters i.e 9
second is no of characters need to display i.e 4
string example = "aaaaaaaaXbbbbXaaaaaa";
string sub = input.Substring(9, 4);
Console.WriteLine("Substring: {0}", sub);
string s = "aaaaaaaaXbbbbXaaaaaa";
string[] words = s.Split('X');
now you can use the foreach loop to get whatever you want.

Check radio button in RadioButtonList based on value read from database [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a RadioButtonList and I would like to select a radio button in this list based on a value read from a database. Once I read the value from the database, how do I then make the radio button in the list become selected?
You can simply assign the value via RadioButtonList.SelectedValue = reader.value; as long as you know that the value is in the list (if it's not in the list, then you will get an exception when that line executes).
Since it sounds like you do not know for sure that reader.value will be one of the options in the RadioButtonList, so you will need to check that first.
if(RadioButtonList.Items.FindByValue(reader.value) != null) {
RadioButtonList.SelectedValue = reader.value;
}
Alternatively, you could handle the exception via a try/catch.
string sSortname = row["GoodsSortName"].ToString().Trim();
foreach (ListItem s in this.rdbSort.Items)
{
if (s.Text == sSortname)
{
s.Selected = true;
break;
}
}
I Used this way ,had sovle this problem

Why does ReSharper think that "thread.Name == null" is always false? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am writing a helper method for conveniently setting the Name of a Thread:
public static bool TrySetName(this Thread thread, string name)
{
try
{
if (thread.Name == null)
{
thread.Name = name;
return true;
}
return false;
}
catch (InvalidOperationException)
{
return false;
}
}
It's working as intended. ReSharper, however, claims that the condition is always false and the corresponding code is heuristically unreachable. That's wrong. A Thread.Name is always null until a string is assigned.
So, why does ReSharper think it is? And is there some way to tell ReSharper it isn't (other than // ReSharper disable ...)?
I'm using ReSharper 5.1.3.
This was fixed in 6+ of RS I think. See here.
It appears to be a bug in R#, fixed in v6.
see: http://devnet.jetbrains.net/message/5366898

C# replace space [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
this is part of my program code for a piglatin translation, so I create a punctuation for my sentence but when i compile it shows the extra place within the sentence. so I do this:
private bool isPunctuation(string noo)
{
int pos = 1;
string punctuation = ",.!?";
pos = punctuation.IndexOf(' ');
while (pos >= 0)
{
pos = punctuation.IndexOf(' ', pos + 1);
//return false;
}
return true;
}
it still show the extra space. what do i need change in this code?
It's not clear how you're trying to use your method anyway, but:
Your method never uses its input (noo)
This code:
string punctuation = ",.!?";
pos = punctuation.IndexOf(' ');
... will always leave pos as -1, as ",.!?" doesn't contain a space
Your method could only exit either via an exception or by returning true; there's no way it can ever return false, as the return false; statement is commented out. (With it uncommented, you've then got a pretty odd while loop...) How were you intending to use it?
Fundamentally, it seems to me that you need to take a step back. Think about why you wanted such a method (it's unclear what it has to do with the replacing part of the question title) and exactly how it should behave.

Extracting double/decimal number from string? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I need to extract the value 33345.002 from the following string:
"ABC(MAX)(33345.002)"
How can I perform this in C#?
I tried handling it in SQL but was picking up the (MAX) too so now I'm gonna try C#.
Thanks
.
.
.
This is the closest so far :
string temp = "YYY(33345.002)(gg)YYYY";
temp = Regex.Replace(temp, "[^.0-9]", "");
double num;
bool success = Double.TryParse(temp, out num);
if (success)
{
//do what ever to the number}
but there is a problem, some of the numbers have zeros in front of them. like: 00033.33
This is really pretty simple.
Declare the characters you want to grap [0-9]/"0123456789" as a constant in C#
loop through the string, example:
public bool TryParseDouble(string input, out double value){
if(string.IsNullorWhiteSpace(input)) return false;
const string Numbers = "0123456789.";
var numberBuilder = new StringBuilder();
foreach(char c in input) {
if(Numbers.IndexOf(c) > -1)
numberBuilder.Append(c);
}
return double.TryParse(numberBuilder.ToString(), out value);
}
Ofcoarse this could be enhanced (perhaps just parse out the first number, or return an array of doubles parsing out all numbers) - not to mention it will parse out multiple decimals which is not exactly what you want.
The same technique can be used in T-SQL as well with looping over the string, declaring the valid values then using 'in'.
EDIT: On second thought
Regex.Match(input, #"\d+(.\d+)?")
would extract double/decimal from string then you could just use double.Parse if a match is found :).
EDIT 2: Btw for some silly reason '\ .' gets escaped as '.' on Stack Overflow. Just note that the decimal in the regex is escaped (just . matches anything)
Happy coding!
You need the same regex (including the enhancements mentioned) as provided as the top answer by J-16 SDiZ for this SO: Regular expression for decimal number, but without the ^ at the beginning and $ at the end.
Next time it might be worth searching a bit on SO or Google first :)

Categories