Is there any possibility to delete specific words from a string? For exempla
string x ="documents\bin\debug" and I want to delete "\bin\debug".
Use String.Replace():
string x = #"documents\bin\debug";
string desiredString = x.Replace(#"\bin\debug", String.Empty);
Note: The key thing here is that you have to assign the string returned by the Replace() function to a variable. (From your comment on the question, it is the problem). This can either be another variable (as in the above example) or the same variable:
string x = x.Replace(#"\bin\debug", String.Empty);
Implicitly, not assigning the return value to a variable (or using the former way) will keep the value of x, unchanged, which is the exact problem you're facing. Hope it helps :)
Use string replace
string x = #"documents\bin\debug";
string nestring = x.Replace(#"\bin\debug", "");
Console.WriteLine(nestring);
Related
I have a string like
string variable1="EXAMPLE";
and later somewhere in my code, I use like
Console.WriteLine(variable1.ToLower());
I may use variable1.ToLower() multiple times. But now I want to store the variablename that is converted to Lower in a separate new variable, that is, I have to extract variable1 from Console.WriteLine(variable1.ToLower()); line and store it in a string variable. Is that possible?
My main goal is that, If my code has variable1.ToLower() in too many places, then I have to run an application, that replaces all variable1.ToLower() to a new string that has the value of variable1.ToLower(). Please Note that using too many variable1.ToLower() in a code is a violation.So I am just creating a new variable to store the value of variable1.ToLower() and use that new variable instead of variable1.ToLower() in every place.
Assuming I understand the question, why not just do this?
var lower = variable1.ToLower();
Console.WriteLine(lower);
String.ToLower creates a copy of the original string. So the original string is not modified and you can safely use it otherwise.
string variable1 = "EXAMPLE";
string lowerCaseVariable1 = variable1.ToLower();
Console.WriteLine($"Is still the original string: {variable1}");
Console.WriteLine($"Is the lower case copy of the original string: {lowerCaseVariable1}");
EDIT:
If you want to get the name of the string variable instead of the content, you can use nameof (Link).
string variable1 = "EXAMPLE";
string nameOfVariable1 = nameof(variable1);
Console.WriteLine(variable1.ToLower());
I have a string as
"cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc_197"
I want to fetch the last digits(i.e. 197)
For obtaining same i have implemented below code
int lastUnderscore = addUcoid.LastIndexOf('_');
string getucoid = addUcoid.Substring(lastUnderscore).TrimStart('_');
The getucoid string gets the digit part properly.The problem now is that I have to also check if the digits occur in string,i.e. string can be like
"cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc"
How to perform the check on such string, whether that part(197) exists at the end in the string or not.
Here 197 is just an example.It could be any numeric data,for example 196,145,etc
string.Contains won't help: we know it is there at least once already. I would use:
int otherLocation = addUcoid.IndexOf(getucoid);
and compare this to lastUnderscore. If otherLocation is non-negative and less than lastUnderscore, then it is there in an earlier position too. You could also use:
int otherLocation = addUcoid.IndexOf(getucoid, 0, lastUnderscore);
and compare to -1; this second approach stops at the underscore, so won't find the instance from the end.
I think Regex is the easiest way. If match.Success is true the digits have been found.
Match match = System.Text.RegularExpressions.Regex.Match(addUcoid, #"(?<=_)\d+$");
if(match.Success)
{
int i = int.Parse(match.Value);
}
I suspect you just want to know if this last part is a numeric or not:
bool isNumeric = Regex.IsMatch(getucoid, #"^\d+$");
Use String.EndsWith method to findout.
http://msdn.microsoft.com/en-us/library/2333wewz%28v=vs.110%29.aspx
string s="cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc";
bool isends=s.EndsWith("197");//returns false;
s="cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc_197";
isends=s.EndsWith("197");//returns true;
The presence of a substring can be checked with String.Contains(): http://msdn.microsoft.com/en-us/library/dy85x1sa%28v=vs.110%29.aspx
Assuming you have removed the final "_197" after checking that it exists, calling Contains("197") on your string will return true if "197" is a substring, or false if it isn't.
You can use LINQ (also to check if the last char is a digit):
string str = "cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc_197";
if (Char.IsDigit(str.Last()))
{
string digits = new string(str.Reverse().TakeWhile(c => Char.IsDigit(c)).Reverse().ToArray());
}
When you say exists at the "end in the string", do you mean if it exists anywhere in the string? You can do a .Contains() to get it.
bool inTheString = addUcoid.Contains(getucoid);
Also:
FYI, your code:
int lastUnderscore = addUcoid.LastIndexOf('_');
string getucoid = addUcoid.Substring(lastUnderscore).TrimStart('_');
can be simplified shortened into:
string getucoid = addUcoid.Split('_').Last();
EDIT:
Ehm, Marc Gravell rightly points out that the string is there anyway. If you want to find out if another instance of the string is there (aside from getucoid):
int lastUnderscore = addUcoid.LastIndexOf('_');
string getucoid = addUcoid.Split('_').Last();
bool inTheString = addUcoid.Substring(0,lastUnderscore).Contains(getucoid);
suppose there is a string like this
string temp2 = "hello";
char[] m = { 'u','i','o' };
Boolean B = temp2.Compare(m);
I want to check if the string contains my array of character or not?
I am trying but it is not taking.On compiling the message
temp2.Compare(m) should be String type
is coming.
Means it follows string.compare(string);
I hope it is not the way there should be some way to do that.
edit//
I have Corrected the line String.Compare return the Boolean Value
If what you want to determine is whether the string contains any of the characters in your array, you can use the string.IndexOfAny function.
bool containsAny = temp2.IndexOfAny(m) >= 0;
I currently get the users number like this:
if (checkBox1.Checked)
{
rchars = numericUpDown1.Value.ToString();
}
else
{
rchars = "3";
}
string rchars; is a global variable.
So, I'm trying to remove the rchars from file names.
For example the first three characters from the file name:
int num = Int32.Parse(rchars);
foreach (FileInfo name in fpaths.GetFiles("*.mp3")
{
string snub = name.Name.Substring(num);
MessageBox.Show(snub);
System.IO.File.Move(blah + name.Name, newblah + snub);
}
My question is how can I get "num" to work in a substring? Since I can't get it to be a value. Since I want to pass it from the numericUpDown. Add make "num" a value so I can remove the value from the file names.
Thanks.
Value property of NumericUpDown is Decimal - that is perhaps why you are having issues. In your if block, I would consider casting the Value Property of NumericUpDown object into an integer in the true part and use integer value 3 in its else part. There after, I would avoid parsing again and give it to Substring as is.
You say you want to remove all occurrences of rchars from name, so why are you using Substring? If you want to remove the string rchars from name then just keep it as a string and use String.Replace:
string snub = name.Name.Replace( rchars, String.Empty );
Also, the Value property of a NumericUpDown is a decimal, not an int.
are you looking to replace the value "3" within the name of a file with something else/remove it? Like this:
original file:
string fiename = "myfile3.mp3";
remove/replace selected number (in this case 3):
string num = "3";
filename.replace(num, "");
should end up with a filename "myfile.mp3"
I am adding a string (a) to another string (b) before I store it in DB, when I retrieve the value from DB, I want to remove the string(b) from string (a). string (b) is a constant. How can I do it
string a= "text1";
string b="text2";
string c = a+b;
I want to remove b from c after I retrive it from db
c = c.Replace(b, "");
Would be a simple way to do so.
Rather than do any of that, create a computed column in the DB that has the extra text.
Less storage; less code.
Try String.Replace - MSDN documentation here.
As #SvenS has pointed in #Khaled Nassar answer, using String.Replace won't work "as is" in your situation.
One acceptable solution may #Mitch's one, but if you don't have that access to modify your database, maybe there's another solution in pure C#:
int indexOfB = c.LastIndexOf(b);
string cWithoutB = c;
if(indexOfB >= 0)
{
c.Substring(0, indexOfB);
}
This prevents replacing more than once the same string as b, because who knows if some user save the same text as b and logic shouldn't be removing it if it's not the one predefined by your application.