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);
Related
Code first:
string myString = "<at>onePossibleName</at> some question here regarding <at>disPossibleName</at>"
// some code to handle myString and save it in myEditedString
Console.WriteLine(myEditedString);
//output now is: some question here regarding <at>disPossibleName</at>
I want to remove <at>onePossibleName</at> from myString. The string onePossibleName and disPossbileName could be any other string.
So far I am working with
string myEditedString = string.Join(" ", myString.Split(' ').Skip(1));
The problem here would be that if onePossibleName becomes one Possible Name.
Same goes for the try with myString.Remove(startIndex, count) - this is not the solution.
There will be different method depending on what you want, you can go with a IndexOf and a SubString, regex would be a solution too.
// SubString and IndexOf method
// Usefull if you don't care of the word in the at tag, and you want to remove the first at tag
if (myString.Contains("</at>"))
{
var myEditedString = myString.Substring(myString.IndexOf("</at>") + 5);
}
// Regex method
var stringToRemove = "onePossibleName";
var rgx = new Regex($"<at>{stringToRemove}</at>");
var myEditedString = rgx.Replace(myString, string.Empty, 1); // The 1 precise that only the first occurrence will be replaced
You could use this generic regular expression.
var myString = "<at>onePossibleName</at> some question here regarding <at>disPossibleName</at>";
var rg = new Regex(#"<at>(.*?)<\/at>");
var result = rg.Replace(myString, "").Trim();
This would remove all 'at' tags and the content between. The Trim() call is to remove any white space at the beginning/end of the string after the replacement.
string myString = "<at>onePossibleName</at> some question here regarding <at>disPossibleName</at>"
int sFrom = myString.IndexOf("<at>") + "<at>".Length;
int sTo = myString.IndexOf("</at>");
string myEditedString = myString.SubString(sFrom, sFrom - sTo);
Console.WriteLine(myEditedString);
//output now is: some question here regarding <at>disPossibleName</at>
Thank you all for the help I have got it working with your help.
So I have written some code which extracts the first word within a string. Below is my code.
var LongString = "Hello World";
var firstWord = LongString.Substring(0, LongString.IndexOf(" "));
This code gives me the result "Hello" however how can I retrieve the last word from the string if I do not know the last index. Is there a method in which I can get the last index number without feeding it with a string that is currently within the LongString variable. Thanks in advance.
var lastWord = longString.Split(' ',
StringSplitOptions.RemoveEmptyEntries)
.Last();
That's about it.
Just use LastIndexOf.
const string hw = "Hello World";
var lastIndex = hw.LastIndexOf(" ");
Console.WriteLine(hw.Substring(lastIndex + 1));
If I understood correct you are looking for:
index = lastIndexLongString.LastIndexOf(" ");
var firstWord = LongString.Substring(index+1);
answer with running fix: use ' ' instead of " " for split function.
var splittedWord = LongString.Split(' ');
var firstWord = splittedWord.FirstOrDefault();
var secondWord = splittedWord.LastOrDefault();
New to Regular Expressions, I want to have the following text in my HTML and would like to replace with something else
Example HTML:
{{Object id='foo'}}
Extract the id into a variable like this:
string strId = "foo";
So far I have the following Regular Expression code that will capture the Example HTML:
string strStart = "Object";
string strFind = "{{(" + strStart + ".*?)}}";
Regex regExp = new Regex(strFind, RegexOptions.IgnoreCase);
Match matchRegExp = regExp.Match(html);
while (matchRegExp.Success)
{
//At this point, I have this variable:
//{{Object id='foo'}}
//I can find the id='foo' (see below)
//but not sure how to extract 'foo' and use it
string strFindInner = "id='(.*?)'"; //"{{Slider";
Regex regExpInner = new Regex(strFindInner, RegexOptions.IgnoreCase);
Match matchRegExpInner = regExpInner.Match(matchRegExp.Value.ToString());
//Do something with 'foo'
matchRegExp = matchRegExp.NextMatch();
}
I understand this might be a simple solution, I am hoping to gain more knowledge about Regular Expressions but more importantly, I am hoping to receive a suggestion on how to approach this cleaner and more efficiently.
Thank you
Edit:
Is this an example that I could potentially use: c# regex replace
While I am not solving my initial question with Regular Expressions, I did move into a simpler solution using SubString, IndexOf and string.Split for the time being, I understand that my code needs to be cleaned up but thought I would post the answer that I have thus far.
string html = "<p>Start of Example</p>{{Object id='foo'}}<p>End of example</p>"
string strObject = "Slider"; //Example
//When found, this will contain "{{Object id='foo'}}"
string strCode = "";
//ie: "id='foo'"
string strCodeInner = "";
//Tags will be a list, but in this example, only "id='foo'"
string[] tags = { };
//Looking for the following "{{Object "
string strFindStart = "{{" + strObject + " ";
int intFindStart = html.IndexOf(strFindStart);
//Then ending in the following
string strFindEnd = "}}";
int intFindEnd = html.IndexOf(strFindEnd) + strFindEnd.Length;
//Must find both Start and End conditions
if (intFindStart != -1 && intFindEnd != -1)
{
strCode = html.Substring(intFindStart, intFindEnd - intFindStart);
//Remove Start and End
strCodeInner = strCode.Replace(strFindStart, "").Replace(strFindEnd, "");
//Split by spaces, this needs to be improved if more than IDs are to be used
//but for proof of concept this is perfect
tags = strCodeInner.Split(new char[] { ' ' });
}
Dictionary<string, string> dictTags = new Dictionary<string, string>();
foreach (string tag in tags)
{
string[] tagSplit = tag.Split(new char[] { '=' });
dictTags.Add(tagSplit[0], tagSplit[1].Replace("'", "").Replace("\"", ""));
}
//At this point, I can replace "{{Object id='foo'}}" with anything I'd like
//What I don't show is that I go into the website's database,
//get the object (ie: Slider) and return the html for slider with the ID of foo
html = html.Replace(strCode, strView);
/*
"html" variable may contain:
<p>Start of Example</p>
<p id="foo">This is the replacement text</p>
<p>End of example</p>
*/
I have a string like following,
string myline="public methodname(parameters)";
How can I insert a new string, like "static" at the first occurence space, that is between public and methodname.
Note that the first word and second word of my string can be anything. And I want to insert a string at the first space in my string.
so my output will be like
public static methodname(parameters)
I have used, Insert and IndexOf methods. But I cannot get the exact result. Please help. Thanks in Advance
Here you go
string myline = "public methodname(parameters)";
string result = myline.Insert(myline.IndexOf(' '), " static");
Or you can try Replace
string myline = "public methodname(parameters)";
string result = myline.Replace("public ", "public static ")
.NET Fiddle
Example:
string s = "Dot Net ";
string v = s.Replace("Net", "Basket");
Something like this, I haven't tested previous version of the code, follow should be OK.
String myline = "Public something";
int pos = myline.IndexOf(" ");
if(pos < 0)
{
//Error
}
String stringYouWant = myline.Substring(0, pos) + " static " + myline.Substring(pos +1);
Console.WriteLine(stringYouWant);
The below string is coming from a DIV tag. So I have enclosed the value below.
String cLocation = "'target="_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'"
I would like to replace in the above string by changing "src="/" with "src='xyz/files'".
I have tried the typical string.Replace("old","new") but it didn't work.
I tried the below,
cNewLocation ="xyz/files";
cNewString = cLocation.Replce("src='/'", "src='" + cNewLocation + "'/")
It didn't work.
Please suggest.
If I'm understanding what you're asking, you could use Regex to replace the string like so:
var cNewString = Regex.Replace(cLocation, #"src='/.*/", "src='" + newLocation + "/");
EDIT : I modified the regular expression to replace src='/.../ with src='{newLocation}/
you might try looking at the Replace command in c#.
so mystring = srcstring.Replace("old", "New");
http://msdn.microsoft.com/en-us/library/system.string.replace%28v=vs.71%29.aspx
possible replace the / in the string with //?
You can do the following:
string cLocation = "'target='_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'";
cLocation = cLocation.Replace("src='/'", "src='xyz/files'");
This fixes the problem:
int start = cLocation.IndexOf("src='") + 5;
int end = cLocation.LastIndexOf("'");
string xcLocation = cLocation.Remove(start, end - start);
string cLocation = xcLocation.Insert(start , "xyz/files");