C# replace space [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 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.

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.

Position in a string C# [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 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.
}

Using substring in line C# [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'm having problems with the substring method, getting this error.
"Index and length must refer to a location within the string."
"Parameter name: length"
string[] nombre = item.Split(new char[]{' '});
this.listBox5.Items.Add(nombre[0].Substring(0,2).ToUpper()+nombre[1].Substring(0,1));
It means that the values you are passing to Substring are not valid for the string they are being called on. For example:
string s = "hello";
string x = s.Substring(0, 1); // <-- This is fine (returns "h")
string y = s.Substring(1, 3); // <-- Also fine (returns "ell")
string z = s.Substring(5, 3); // <-- Throws an exception because 5 is passed
// the end of 's' (which only has 5 characters)
As an aside, I see this a lot:
item.Split(new char[]{' '})
I think people are confused by the signature of the Split method. The following is sufficient:
item.Split(' ')

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 :)

C# check line number of stringreader [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.
How do I check if the string reader has passed a certain line number, or has passed a line number which contains some text? I put this in the line processing code of a string reader:
if (currentline.Contains("123"))
currentbank = "123";
else if (currentline.Contains("456"))
currentbank = "456";
else if (currentline.Contains("789"))
currentbank = "789";
I want to change the contents of a string based on what range of line numbers it is in, with my code it always gives 123. Like for example if it's from lines 10-20 (or from 123 to 456) then the string should have 123, 20-30 (or 456 to 789) it should have 456 and 30-40 have 789. How can I do this using a StringReader?
Fixed it myself, problem being I used upper case (e.g. TEST) instead of lower case.

Categories