Want to assign HTML value to String variable - c#

I want to assign HTML snippet to string variable.
something like -
string div = "<table><tr><td>Hello</td></tr></table>"; // It should return only 'Hello'
Please suggest.

string div = "<table><tr><td>Hello</td></tr></table>"; // It should return only 'Hello
var doc = new XmlDocument();
doc.LoadXml(div);
string text = doc.InnerText;
Do you also need the Jquery version of this?

If you are sure that the HTML won't change between the string you want to get, you can simply do a Substring between the two constants string and you will get your string into your variable.
const string prefix = "<table>";
const string suffix = "</table>";
string s = prefix + "TEST" + suffix ;
string s2 = s.Substring(prefix.Length, s.IndexOf(suffix, StringComparison.Ordinal) - prefix.Length);
Here is the Regex version:
const string prefix = "<table>";
const string suffix = "</table>";
string s = prefix + "TEST" + suffix;
string s2 = Regex.Match(s, prefix + "(.*)" + suffix).Groups[1].Value;

Related

Number format in Word document

I have some fields in an Word document with instructional text, for example like this one:
{ MERGEFIELD MyField1 \# "000" }
I'm reading those fields using OpenXML SDK, as shown here, and replacing them with some data.
I'm applying the number format that is specified in the field to my data, like this:
string fieldFormat = "000";
int data = 1234;
string fieldValue = data.ToString(fieldFormat);
In most cases this works great, but now I got a document that has "xxx" as a number format and when using the above I get "xxx" for fieldValue.
string fieldFormat = "xxx";
int data = 1234;
string fieldValue = data.ToString(fieldFormat); // Problem!
I saw that .NET doesn't support this in Custom numeric format, but Word supports this in a way that it removes any digits that come after the "x" symbols place.
So for the above, it should result in "234". How can I do that in C#?
EDIT, added some additional examples:
int number = 1158;
string format1 = "x";
string format2 = "xx";
string format3 = "x#";
string format4 = "x,###";
string format5 = "x,xxx";
string format6 = "x##.00";
string format7 = "xxxxxxx.00";
Console.WriteLine("(number + format1) should result to: 8");
Console.WriteLine("(number + format2) should result to: 58");
Console.WriteLine("(number + format3) should result to: 58");
Console.WriteLine("(number + format4) should result to: 1,158");
Console.WriteLine("(number + format5) should result to: 1,158");
Console.WriteLine("(number + format6) should result to: 158.00");
Console.WriteLine("(number + format7) should result to: 1158.00");

Find substring between slashes and replace that part in ASP.NET C#

Using ASP.NET C#, I need to find and replace the word string1 between the last two slashes and replace with string2
Example:
string fullStr = "/this/is/string1/part";
string subStr = "function";
string finalStr = "/this/is/" + subStr + "/part";
And a regex solution:
string fullStr = "this/is/string1/part";
string subStr = "function";
var newstr = Regex.Replace(fullStr, #"/[^/]+/(?=[^/]+$)", m => "/" + subStr + "/");
I don't feel a need of regex here.
string fullStr = "/this/is/string1/part";
string subStr = "function";
string[] fullStrParts = fullStr.Split('/');
fullStrParts[fullStrParts.Length - 2] = subStr;
string finalStr = string.Join("/", fullStrParts);

Creating function syntax by manipulating string

I am trying to create dynamic syntax function and function syntax is like below:
MyFunction( arg1,arg2,ar3.....);
I have string like this:
str = Previousvalue.Value1,Previousvalue.Value2
Now I would like to create syntax like this in final variable :
String final = MyFunction(Previousvalue.Value1,',',Previousvalue.Value2);
str = Previousvalue.Value1,Previousvalue.Value2,Previousvalue.Value3;
String final = MyFunction(Previousvalue.Value1,',',Previousvalue.Value2,',',Previousvalue.Value3);
This is how I am trying to achieve with string.join (without using loop) but not getting how to do it and this seems like impossible to do without using loop:
final = string.Join("MyFunction(", str.Split(','));
Case 1:
Input : string str =Previousvalue.Value1,Previousvalue.Value2
Output:
string final=MyFunction(Previousvalue.Value1,',',Previousvalue.Value2,',',Previousvalue.Value3);
Case 2 :
Input : str = Previousvalue.Value1,Previousvalue.Value2,Previousvalue.Value3;
output:
String final = MyFunction(Previousvalue.Value1,',',Previousvalue.Value2,',',Previousvalue.Value3);
Case 3:
string input = " Previousvalue.Value1";
Output:
String final = Previousvalue.Value1; //No function
From what I understand, you want to generate a string like this:
"MyFunction(Previousvalue.Value1,',',Previousvalue.Value2);"
^..........^...................^....^...................^.
prefix arg1 sep arg2 suffix
or in other words
prefix = "MyFunction(";
separator = ",',',";
suffix = ");"
which can be achieved by moving the prefix and suffix out of the string.Join and using the above separator value:
string final = "MyFunction(" + string.Join(",',',", str.Split(',')) + ");";
Also instead of Split / Join you could simply use string.Replace:
string final = "MyFunction(" + str.Replace(",", ",',',") + ");";
From my understanding of the problem you want to call the MyFunction method with n string parameters, but also with string[]. You can do it like this:
public static void Main(string[] args)
{
string str = "Test1,Test2,Test3";
string test1 = MyFunction("Test1", "Test2", "Test3");
string test2 = MyFunction(str.Split(','));
}
public static string MyFunction(params string[] parameters)
{
StringBuilder sb = new StringBuilder();
foreach(var item in parameters)
{
sb.AppendLine(item);
}
return sb.ToString();
}
Try :
public string MyJoin(params string[] vars)
{
return "MyFunction(" + string.Join(",", vars) + ");";
}

HttpContext.Current.Request.Url How to get the part after application name from URL

var myPath = HttpContext.Current.Request.Url.AbsolutePath;
// output: myApplication/myFolder/myPage.aspx
var pageName = Path.GetFileName(myPath);
//output: myPage.aspx
I am trying to output "myFolder/myPage.aspx" without the application path.
Is there built-in option to return that or I would need to use regular expression to get what I need?
Thanks
You should be able to make use of HttpContext.Current.Request.Url.Segments and then a simple string concat:
String[] segments = HttpContext.Current.Request.Url.Segments;
string result = segments[1] + segments[2];
or instead of string concat, use: string result = Path.Combine(segments[1],segments[2]);
This should work
public ActionResult oihoi(string ImageName
{
string _FileName = Path.GetFileName(ImageName.FileName);
string folderpath = "UploadedFiles/WebGallery";
string path = Server.MapPath("~/" + folderpath);
string firstsegment = "";
}

How can I split a string to obtain a filename?

I am trying to split the string. Here is the string.
string fileName = "description/ask_question_file_10.htm"
I have to remove "description/" and ".htm" from this string. So the result I am looking for "ask_question_file_10". I have to look for "/" and ".htm" I appreciate any help.
You can use the Path.GetFileNameWithoutExtension Method:
string fileName = "description/ask_question_file_10.htm";
string result = Path.GetFileNameWithoutExtension(fileName);
// result == "ask_question_file_10"
string fileName = Path.GetFileNameWithoutExtension("description/ask_question_file_10.htm")
try
string myResult = fileName.SubString (fileName.IndexOf ("/") + 1);
if ( myResult.EndsWith (".htm" ) )
myResult = myResult.SubString (0, myResult.Length - 4);
IF it is really a path then you can use
string myResult = Path.GetFileNameWithoutExtension(fileName);
EDIT - relevant links:
http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx
http://msdn.microsoft.com/en-us/library/hxthx5h6.aspx
http://msdn.microsoft.com/en-us/library/2333wewz.aspx
http://msdn.microsoft.com/en-us/library/system.string.length.aspx
string fileName = "description/ask_question_file_10.htm";
string name = Path.GetFileNameWithoutExtension(fileName);

Categories