I want to replace all the " and ' from a string
eg strings
"23423dfd
"'43535fdgd
""'4353fg
""'''3453ere
the result should be
23423dfd
43535fdgd
4353fg
3453ere
I tried this myString.Replace("'",string.Empty).Replace('"',string.Empty); but its not giving me the correct result.
Use String.Replace
mystring = mystring.Replace("\"", string.Empty).Replace("'", string.Empty)
Do two replaces:
s = s.Replace("'", "").Replace("\"", "");
Try this:
string s = yoursting.Replace("\"", string.Empty).Replace("'", string.Empty);
Related
I need to replace the string "AAAA:123346hadhdhajkkd890" with "AAAA":"123346hadhdhajkkd890" using the Replace function in C#.
Hi try to use the code below :
var str = "AAAA:123346hadhdhajkkd890";
str = str.Replace(":", "\":\"");
in order to put " in Replace function you have to use \" in the parameter.
var s1 = "AAAA:123346hadhdhajkkd890";
var s2 = str.Replace(":", "\":\"");
This is because \" will actually tell the compiler " and if simply " will mean the end of the string. This is known as the escape sequence.
Imagine I have a string like:
xxxstrvvv string xxxstringvvv str I am string for testing.
I want to find and replace all instances of str with xxxstrvvv that are not already contained in a xxxvvv.
so the result would be:
xxxstrvvv xxxstrvvving xxxstringvvv xxxstrvvv I am xxxstrvvving for testing
Anyone know an easy way to do this?
Edit: I want to add another situation to clarify.
xxxabcstrefgvvv
it should NOT replace this because the str is contained in xxxvvv
I suggest using regular expression with negative looking ahead and behind:
string source = "xxxstrvvv string xxxstringvvv str I am string for testing.";
string result = Regex.Replace(source, #"(?<!xxx)str(?!vvv)", "xxxstrvvv");
Edit: Same method, but a bit different pattern for the edited question:
string result = Regex.Replace(
source,
#"(?<!xxx[a-zA-Z]*)str(?![a-zA-Z]*vvv)", "xxxstrvvv");
Outcomes:
source = "xxxstrvvv string xxxstringvvv str I am string for testing.":
xxxstrvvv xxxstrvvving xxxstringvvv xxxstrvvv I am xxxstrvvving for testing.
source = "xxxabcstrefgvvv":
xxxabcstrefgvvv
Ok, I agreed with the answer of Dmitry Bychenko about Regular Expressions.
But, if your request is limited to the requirement on your answer we can use this code:
string val = "xxxstrvvv string xxxstringvvv str I am string for testing.";
val = val.Replace("xxxstringvvv", "str");
val = val.Replace("str","xxxstringvvv");
I'd go with the regex, but if you want to use replaces, this would work, if you don't have "xxxxxxstrvvvvvv" in your initial string and want to keep them that way:
string findString = "str";
string addBefore = "xxx";
string addAfter = "xxx";
string myString = "xxxstrvvv string xxxstringvvv str I am string for testing.";
myString = myString.Replace(findString, addBefore+findString+addAfter);
myString = myString.Replace(addBefore+addBefore+findString+addAfter+addAfter, addBefore+findString+addAfter);
Yes; it is ugly. I just basically do that in Notepad++ all the time with ctrl-H.
I have written a script in Python. I think you would be able to convert it to C#.
one_line = 'xxxstrvvv string xxxstringvvv str I am string for testing'
final_str = ""
arry_len = one_line.split(" ")
for ch in arry_len:
if 'str' in ch:
if not 'xxxstrvvv' in ch:
ch = ch.replace("str","xxxstrvvv")
final_str = final_str + " " + ch
print final_str
I have an String like this "00.00-0-00" and I need to transform like this "0000-0/00"
I already tried:
field.ToString("####-##/##") ' without success
String.Format(CultureInfo.InvariantCulture, "{0:####-#/##}", field) 'without success
Any help with that?
My version which work if your original format doesn't change(as other answers based too)
Dim oldString As String = "00.00-0-00"
Dim parts As String() = oldString.Split({"."c, "-"c})
Dim result As String = _
String.Format("{0}{1}-{2}/{3}", parts(0), parts(1), parts(2), parts(3)))
C# version
string original = "00.00-00-00";
string[] parts = original.Split(new char[] {'.', '-'});
string result = String.Format("{0}{1}-{2}/{3}", parts[0], parts[1], parts[2], parts[3]));
Here is how I would do it (val is input string)
string.Format(#"{0}{1}-{2}/{3}",
val.Substring(0,2),
val.Substring(3,2),
val.Substring(6,1),
val.Substring(8,2));
If you don't like string.Format you could also use concatenation (+)
If your format is always "00.00-0-00", then you can use Regex.Replace() like this:
VB.NET
Dim data As String = "00.00-0-00"
Console.WriteLine(Regex.Replace(data, "(\d+)\.(\d+)-(\d+)-(\d+)", "$1$2-$3/$4"))
C#
string data = "00.00-0-00";
Console.WriteLine(Regex.Replace(data, "(\\d+)\\.(\\d+)-(\\d+)-(\\d+)", "$1$2-$3/$4"));
Results:
0000-0/00
The Regex is capturing all of the digits into groups and you just put the groups back together with whatever delimiter you want.
If you know that your string will always be in the format, you could just do it manually. If you need to do this a lot, you could just create a function to do it.
Dim oldString As String = "00.00-0-00"
Dim newString As String = MakeNewString(oldString)
Function to call:
Function MakeNewString(oldString As String) As String
Dim noPeriod As String = Replace(oldString, ".", "")
Return Split(noPeriod, "-")(0) & "-" & Split(noPeriod, "-")(1) & "/" & Split(noPeriod, "-")(2)
End Function
In C#:
string oldString = "00.00-0-00";
string newString = MakeNewString(oldString);
C# Function:
public string MakeNewString(string oldString)
{
string noPeriod = Strings.Replace(oldString, ".", "");
return Strings.Split(noPeriod, "-")(0) + "-" + Strings.Split(noPeriod, "-")(1) + "/" + Strings.Split(noPeriod, "-")(2);
}
I have a string variable and following is the content of it:
.....
DataElement deAbtVersionNum
m_AttrParent commercialcardsys::CommercialCardInt
m_AttrGUIFieldLabel "WEX_CI 3.02.01P20.1" appsys30::lngDbb
m_AttrdbType "char"
.....
As the ... indicates, there maybe other text also.
In the third line we have "WEX_CI 3.02.01P20.1" (This is the only place starting from bottom where WEX.. is present.)
I need to replace 3.02.01P20.1(entirely) with a new version say 3.02.01P20.1.NEW
I have been able to do it using a dirty method which looks for the index of "Wex and then finds the next " and blah blah.
int start = CItext.LastIndexOf("\"WEX") + 1;
int end = CItext.IndexOf("\"", start);
string text = CItext.Substring(start, end - start + 1);
string[] parts = text.Split(new Char[] { ' ' });
string editedText = parts[0] + " " + LabelName;
CItext = CItext.Replace(text, editedText);
CIText is the string that I have to edit.
LabelName is the string I want to put instead of 3.02.01P20.1
Can anyone suggest me any other clean method ?
Try This Regex
var result = Regex.Replace(text,#"(WEX_CI[\s][\da-zA-Z\.]+)","$1.NEW");
I think you can use a regex with "lookahead". Try this.
var result = Regex.Replace(text, "(?<=WEX_CI )[^\"]+", "NEW", RegexOptions.Multiline);
My url contains all' + 's like path/My+Property+Details but I need to replace all + with '-'.and make it:
path/My-Property-Details.
Use String.Replace(..), like so:
string s = "path/My+Property+Details";
s = s.Replace("+", "-");
Don't forget the assignment because a string is immutable.
Use String.Replace.
url = url.Replace("+", "-");
Try this:
string newURL = oldUrl.Replace("+", "-");