I have the string in format of Rs.2, 50,000. Now I want to read the string value as 250000 and insert into database (let assume that Rs 2,50,000 coming from third party like web services, wcf services, or from other application ).Is There any direct solution for this without using
String.Replace() can u write the code to remove the commas.
Something like this works for me
string numb="2,50,000";
int.TryParse(numb, NumberStyles.AllowThousands, CultureInfo.InvariantCulture.NumberFormat,out actualValue);
Something like this: (I haven't tested this at all)
string str = "RS.2,50,000";
str = str.substring(3, str.length);
string arr[] = str.split(",");
string newstr;
foreach (string s in arr)
{
newstr += s;
}
Try
string str = "25,000,000";
str = string.Join(string.Empty, str.Split(','));
int i = Convert.ToInt32(str);
Try this:
string rs = "Rs.2, 50,000";
string dst="";
foreach (char c in rs)
if (char.IsDigit(c)) dst += c;
rs is source string, wihle in dst you have string you need.
Something a little different:
string rs = "Rs.2, 50,000";
string s = String.Join("", Regex.Split(rs, #"[^\d]+"));
Related
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) + ");";
}
I need to convert a string i.e. "hi" to & #104;& #105; is there a simple way of doing this? Here is a website that does what I need. http://unicode-table.com/en/tools/encoder/
Try this:
var s = "hi";
var ss = String.Join("", s.Select(c => "&#" + (int)c + ";"));
Try this:
string myString = "Hi there!";
string encodedString = myString.Aggregate("", (current, c) => current + string.Format("&#{0};", Convert.ToInt32(c)));
Based on the answer to this question:
static string EncodeNonAsciiCharacters(string value)
{
StringBuilder sb = new StringBuilder();
foreach (char c in value)
{
string encodedValue = "&#" + ((int)c).ToString("d4"); // <------- changed
sb.Append(encodedValue);
}
return sb.ToString();
}
I have a text called
string path = "Default/abc/cde/css/";
I want to compare a text.
string compare = "abc";
I want a result
string result = "Default/abc";
The rest of the path /cde/css is useless.Is it possible to grab the desire result in asp.net c#. Thanks.
Is this what you looking for?:
string result = path.Substring(0, path.IndexOf(compare)+compare.Length);
Try this. This will loop through the different levels (assuming these are directory levels) until it matches the compare, and then exit the loop. This means that if there is a folder called abcd, this won't end the loop.
string path = "Default/abc/cde/css";
string compare = "abc";
string result = string.Empty;
foreach (string lvl in path.Split("/")) {
result += lvl + "/";
if (lvl == compare)
{
break;
}
}
if (result.Length>0)
{
result = result.substring(0, result.length-1);
}
string path = "Default/abc/cde/css/";
string answer = "";
string compare = "abc";
if (path.Contains(compare ))
{
answer = path.Substring(0, path.IndexOf(stringToMatch) + compare.Length);
}
Something like the above should work.
I suggest that if you meet questions of this kind in the future, you should try it yourself first.
string result = path.Contains(compare) ? path.Substring(0, (path.IndexOf(compare) + compare.Length)) : path;
I have this string in C# -
".... School||Abc\r\n...State||CA\r\n..."
The school and state are somewhere in the string. I need to parse the string in such a way that i get the values of School and State for my parameters
string school = abc (from String after parsing)
string state = CA (from string after parsing)
Try this:
string longStr = "School||Abc\r\nState||CA\r\n";
string[] keyValPairs = s.Split("\r\n".ToCharArray());
Dictionary<string, string> info = new Dictionary<string, string>();
foreach(string pair in keyValPairs)
{
string[] split = pair.Split("||");
//split[0] is the key, split[1] is the value
info.Add(split[0], split[1]);
}
Now you can access what you need like so:
string school = info["School"];
string state = info["State"];
Where the longStr variable is just your long string that you start out with, not neccessarily what I set it to.
Try split-ing string on new line chars and then it looks like a dictionary, with key values separated by ||. Another split on "||" should give you what you want.
Back of the envelope code
private static void ParseMyString(string longString) {
IEnumerable<string> shortStrings = longString.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach(var ss in shortStrings) {
var kvp = ss.Split("||".ToCharArray());
Console.WriteLine("{0} - {1}", kvp[0], kvp[1]);
}
}
You can use the TextFieldParser class to parse this file, in particular if it is fixed width fields or has known delimiters.
It:
Provides methods and properties for parsing structured text files.
Though it lives in the Microsoft.VisualBasic.Text namespace, it is a .NET assembly and can be used in all .NET projects.
Assuming that your string just contains values separated by "||", "\r" or "\n".
string str = "School||Abc\r\n||State||CA\r\n||AA||AA";
str = str.Trim();
str = str.Replace("\r",string.Empty);
str = str.Replace("\n", string.Empty);
string[] keyValue = str.Split(new string[] { "||" }, StringSplitOptions.None);
Dictionary<string, string> KeyValDic = new Dictionary<string, string>();
for (int i = 0; i < keyValue.Length; i++,i++)
{
KeyValDic.Add(keyValue[i], keyValue[i + 1]);
}
Here is the source code for this test:
var tags = new List<string> {"Portland", "Code","StackExcahnge" };
const string separator = " ";
tagString = tags.Aggregate(t => , separator);
Console.WriteLine(tagString);
// Expecting to see "Portland Code StackExchange"
Console.ReadKey();
Update
Here is the solution I am now using:
var tagString = string.Join(separator, tags.ToArray());
Turns out string.Join does what I need.
For that you can just use string.Join.
string result = tags.Aggregate((acc, s) => acc + separator + s);
or simply
string result = string.Join(separator, tags);
String.Join Method may be?
This is what I use
public static string Join(this IEnumerable<string> strings, string seperator)
{
return string.Join(seperator, strings.ToArray());
}
And then it looks like this
tagString = tags.Join(" ")