I have a string with decimal numbers. For some other reason, I want to replace a character instead of "." between the decimal number.
For example,
string str="SAIF Partners had invested $22.5 million in 2006."
What I need is, I want to replace a character between the $22.5 like this $22r!5.
So my final string would be like this,
string final="SAIF Partners had invested $22r!5 million in 2006."
how can I achieve this? Any help would be really appreciated.
I tried the below code. But I think I am missing something.
string final= Regex.Replace(str, #"[^\D]+", "r!");
string input = "SAIF Partners had invested $22.5 million in 2006.";
var output = Regex.Replace(input, #"(\d+)(\.)(\d+)", "$1r!$3");
(?<=\d)\.(?=\d)
Use lookarounds.See demo.Replace by r! or whatever characters you want to put
https://regex101.com/r/sS2dM8/12
var output = Regex.Replace(input, #"(?<=\d)\.(?=\d)", "r!");
Related
Membernumber350005Calculationdate04/08/2015Casenumber880898Case0.12Amount546.3Fors
Here is a string containing words and digits(inluding date and decimal format) in it with no space between them. I want to give space between words and numbers. My output should be a string NOT an array and that should be this..
Membernumber 350005 Calculationdate 04/08/2015 Casenumber 880898 Case 0.12 Amount 546.3 Fors
I tried the below code. I have succeeded filtering date and decimal formats and giving space around them. but i am stuck in giving space around the words.
string s = "Membernumber350005Calculationdate04/08/2015Casenumber880898Case0.12Amount546.3Fors";
string SpaceAroundDate = Regex.Replace(s, #"(\d+/\d+/\d+)", #" $1 ");
string SpaceAroundDecimal = Regex.Replace(SpaceAroundDate, #"(\d+\.\d+)", #" $1 ");
With this code, i could make this,
Membernumber35000520Calculationdate 04/08/2015 Casenumber880898Case 0.12 Amount 546.3 fors
And then i wrote this to give space between the words and the numbers,
string SpaceAroundWord = Regex.Replace(SpaceAroundDecimal, #"([A-Za-z]\w+)([0-9]\d+)([A-Za-z]\w+)", #"$1 $2 $3");
But it doesn't work. Hope i have given enough information needed. Thanks in advance.
Here you go
string input = #"Membernumber350005Calculationdate04/08/2015Casenumber880898Case0.12Amount546.3Fors";
string result = Regex.Replace(input, #"([a-zA-Z]+|[0-9]+[0-9/\.]+[0-9]+)", "$& ");
I have a string that is like the following:
string str = hello_16_0_2016;
What I want is to extract hello from the string. As in my program the string part can occur anywhere as it is autogenerated, so I cannot fix the position of my string.
For example: I can take the first five string from above and store it in a new variable.
But as occurring of letters is random and I want to extract only letters from the string above, so can someone guide me to the correct procedure to do this?
Could you just use a simple regular expression to pull out only alphabetic characters, assuming you only need a-z?
using System.Text.RegularExpressions;
var str = "hello_16_0_2016";
var onlyLetters = Regex.Replace(str, #"[^a-zA-Z]", "");
// onlyLetters = "hello"
I'd use something like this (uses Linq):
var str = "hello_16_0_2016";
var result = string.Concat(str.Where(char.IsLetter));
Check it out
Or, if performance is a concern (because you have to do this on a tight loop, or have to convert hundreds of thousands of strings), it'd probably be faster to do:
var result = new string(str.Where(char.IsLetter).ToArray());
Check it too
But as occurring of letters is random and I want to extract only
letters from the string above, so can someone guide me to the correct
procedure to do this?
The following will extract the first text, without numbers anywhere in the string:
Console.WriteLine( Regex.Match("hello_16_0_2016", #"[A-Za-z]+").Value ); // "hello"
(Apologies, I'm very new to c#)
Given the following string:
SELECT * FROM TABLE WHERE sDATE ='~~Date~~' AND sName = '~~Some NAME~~'
I need to extract Date & Some NAME from the above string into an array.
It's not always going to be Date and Some NAME inside the ~~
There maybe a single ~~x~~ in the string, there maybe two or more
~~x~~ could be any length and may contain numbers, letters and spaces
~~x~~ will always start and end with ~~
~~x~~ may or may not be in quotes
For a given string I'd like to get an array of the found values. I'd be OK with ~~Date~~ or just Date
I'm thinking this might be a regex situation. I've looked at .Split, .IndexOf, .Contains but none of those get me what I'm looking for since I'm not always looking for the same substring.
Update:
This is not strictly for SQL parsing, that's just a quick example. A string could also be
My name is ~~Some NAME~~ and I'm hunting for some help
Given your description, something like (and assuming there are no ~~ inside the string you want to capture):
~~(.*?)~~
would give you the text between a pair of ~~ in a capture group. You could change . to something more restrictive if you want to.
Example: https://dotnetfiddle.net/t6i7rx
var s = "SELECT * FROM TABLE WHERE sDATE ='~~Date~~' AND sName = '~~Some NAME~~'";
Regex r = new Regex(#"~~(.*?)~~");
foreach (Match m in r.Matches(s)) {
Console.WriteLine(m.Groups[1]);
}
Outputs:
Date
Some NAME
Note the importance of *? versus just * here. * by itself is greedy and will match as much as possible. So it would return Date~~' AND sName = '~~Some NAME because it'll take everything between the first and the last ~~. Adding the ? makes it lazy.
I have the following strings:
string a = "1. testdata";
string b = "12. testdata xxx";
What I would like is to be able to extract the number into one string and the characters following the number into another. I tried using .IndexOf(".") and then remove, trim and
substrings. If possible I would like to find something simpler as I have this to do in a
lot of parts of my code.
if the format is always the same you could do:
a.Split('.');
Proposed solutions so far are not correct.
First, after Split('.') or Split(".") you will have space in the beginning of second substring.
Second, if you have more than one dot - you'll have to do something yet after the split.
More robust solution is below:
string a = "11. Test string. With dots.";
var res = a.Split(new[] {". "}, 2, StringSplitOptions.None);
string number = res[0];
string val = res[1];
Argument 2 specifies maximum number of strings to return. Thus when you have several dots - it will make a split only at the first.
string[]list = a.Split(".");
string numbers = list[0];
string chars = list[1];
i have a string like 123Prefix1pics.zip
i want to split it into 123 Prefix1 pics.zip and store them in different variables
i m trying to do it in c#,.net
jst litle bit confused on how to use split method
splitArray = Regex.Split(subjectString, #"(?<=\p{N})(?=\p{L})");
will work in C# to split in positions between a number (\p{N}) and a letter (\p{L}).
If you also want to split between a letter and a number, use
splitArray = Regex.Split(subjectString, #"(?<=\p{L})(?=\p{N})|(?<=\p{N})(?=\p{L})");
however, that splits your example too much.
You only want to split that one string? Too easy!
string filename = "123Prefix1pics.zip"
string part1 = "123"
string part2 = "Prefix1"
string part3 = "pics.zip"
Ok this is a joke, but it gives the right answer. Unless you generalise your splitting rule, or provide further examples, we can only guess.
You may be asking to make a string break after a numeral, but again I'm only guessing.
You can start with:
string filename = "123Prefix1pics.zip"
string part1 = filename.Substring(0, 3);
string part2 = filename.Substring(3, 7);
string part3 = filename.Substring(10, 4);
You can also note String.Split() needs a separator argument, like an ; or ,. As you don't have any separator, you can try two approaches:
Make sure all your filenames have same format; this way, you can to use Substring() to break your strings
You can identify a more general pattern, as "numbers, 7 characters plus 4 more characters" and to use a regular expression. This is a more advanced solution and can lead to maintenance problems;
I recommend you to stick with first option.
You can split it like this:
your ip in a string variable like this:
create a char vector
then a string vector
Code:
string theIP="this is string";
char[] separator={' '}; //you can put multiple separators
string[] result = theIP.Split(separator,StringSplitOptions.None);
this means result[0] is "this", result[1] is "is", and so on.
You can find a good tutorial about string splitting here:
http://goldwin-advertising.ro/index.php?option=com_content&view=article&id=10:splitstring&catid=3:howto&Itemid=5
Good luck!
Look like you want to split by fixed size.
So use yourString.Substring(0, 3);