i have string in textbox:
`New-Value = 12,34 -- Old-Values: 12,31,`
what i'd like to do is to get Old-Value so "12,31,"
How can i get from this textbox this specific information do this? So value is between ":" and ","
Tnx
Regex.Match("New-Value = 12,34 -- Old-Values: 12,31,",#"\:(.+)\,").Groups[1].Value.Trim()
const string oldPointer = "Old-Values: ";
var text = "New-Value = 12,34 -- Old-Values: 12,31,";
var old = text.Substring(text.IndexOf(oldPointer) + oldPointer.Length).TrimEnd(',');
Not very clear if this is a fixed (static) format of your string, but by the way:
A simple solution could be:
string str = "New-Value = 12,34 -- Old-Values: 12,31,";
str.Substring(str.IndexOf(':') + 1);
More complex one should involve Regular expressions, like an answer of L.B or others if any.
Related
Pretty sure we all do string.format and define some string in a specifed format.
I have a string which is always formatted in a way like this:
const string myString = string.Format("pt:{0}-first:{1}", inputString);
To get {0}, I can always check for pt:{ and read till }.
But what is the best/recommended way to extract {0} & {1} from the above variable myString ?
A Regex version of answer, but again, assuming your input doesnt contain '-'
var example = = "pt:hello-first:23";
var str = "pt:(?<First>[^-]+)-first:(?<Second>[^%]+)";
var match = new Regex(str).Match(example);
var first = match.Groups["First"].Value;
var second = match.Groups["Second"].Value;
It might be a good idea that you define what your variable can/cannot contain.
Not sure if this is the best way to do it, but it's the most obvious:
string example = "pt:123-first:456";
var split = example.Split('-');
var pt = split[0].Substring(split[0].IndexOf(':') + 1);
var first = split[1].Substring(split[1].IndexOf(':') + 1);
As Shawn said, if you can guarantee that the variables wont contain either : or - this will be adequate.
I'm trying to format a string of 6 digits with the following format:
1234/56.
I'm using the following:
string.Format("123456", "{0000/00}");
Result 123456
Try something like
var number = int.Parse("123456");
var formattedNumber = number.ToString("####/##", CultureInfo.InvariantCulture);
Edit to include zero fill (if required):
var number = int.Parse("123456");
var formattedNumber = number.ToString("####/##", CultureInfo.InvariantCulture).PadLeft(7,'0');
You will still have issues with any number below 10 (i.e "000009") showing up as 0000/9. In which you are probably better off with a substring modification like below.
var text = "123456";
var formattedNumber = text.Substring(0, 4) + "/" + text.Substring(4, 2);
Please try the below code.
string.Format("{0:####/##}",123456)
I tested the code and is working.
For reference on string formatting please visit:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
If you are using numbers I would use #rob-s 's example above. If you are dealing with actual strings then you can use the following code sample.
var value = "123456";
var index = value.Length - 2;
var formattedValue = $"{value.Substring(0, index)}/{value.Substring(index)}";
Console.WriteLine(formattedValue);
Your output will look like what you have described in your question.
1234/56
This might be a problem with Session and not ToString(), I'm not sure.
I have two .aspx pages and I want to pass an IP address from a datatable from one page to the other. When I do this, spaces get added that I don't want. The simple version of the code is this:
first .aspx page
int num = DropDownList1.SelectedIndex;
DataView tempDV = SqlDataSource2.Select(DataSourceSelectArguments.Empty) as DataView;
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2];
Response.Redirect("test.aspx");
test.aspx page
string ipCamAdd = Session["camera"].ToString();
TextBox1.Text = "http://" + ipCamAdd + "/jpg/image.jpg?resolution=320x240";
what I want to print is
http ://ipadd/jpg/image.jpg?resolution=320x240
but what prints out is
http//ipaddress /jpg/image.jpg?resolution=320x240
how can I fix this?
Also, I asked this question hoping someone could tell me why this is happening as well. Sorry for the mistake.
Try this:
string ipCamAdd = Session["camera"].Trim().ToString();
For the valid concern, Session["camera"] could be null, add function such as the following to your code
static string ToSafeString(string theVal)
{
string theAns;
theAns = (theVal==null ? "" : theVal);
return theAns;
}
Then use:
string ipCamAdd = Session["camera"].ToSafeString().Trim();
You can use string.Replace if you just want to get rid of the spaces:
TextBox1.Text = "http://" + (ipCamAdd ?? "").Replace(" ", "") + "/jpg/image.jpg?resolution=320x240";
Trim the result before setting to session.
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2].Trim();
Seems In SQL your data type is char(*) if you convert the data type to varchar and re enter data, you wont get any additional spaces
I have a number like 123456789. I want to display the number like 123-45-6789. How can I achieve this in asp.net with C#?
This number stored in varchar format in database. I just want to display the number in label with proper hypen.
Use basic number-formatting:
yourNumber.ToString("###-##-####");
Update:
Ok, if you already have it as a string, you can split it and add hyphens:
var result = str.Substring(0,3) + "-"
+ str.Substring(3,2) + "-"
+ str.Substring(5);
...or on secont thought, perhaps String.Insert() is simpler:
// insert "-" after position 3, then after position 6 in the returned string.
result = str.Insert(3, "-").Insert(6, "-");
try this...
Regex.Replace("987654321", #"(\d{3})(\d{2})(\d{4})", "$1-$2-$3");
Simply you can use.
var outputString = yourString.Insert(3,"-").Insert(6,"-");
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");