This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 4 years ago.
How to find the second part of the string using substring in c#
I have follwwing string:
string str = "George Micahel R - 412352434";
I wanted the above string as two parts.Before "-" and after "-"
I have my first part using following code:
string firstPart = str.Substring(0,str.IndexOf(" - "));
When I am trying for the second part using the following code it gives me an error.
string secondPart = str.Substring(str.IndexOf(" - "), Len(str);
How to get the second part(only number)?
Thanks in advance.
Simply use split function:
var splittedArray = str.Split('-');
var leftPart = splittedArray[0];
var rightPart = splittedArray[1];
Use the Split() method instead.
string str = "George Micahel R - 412352434";
string[] words = str.Split('-');
Console.WriteLine(words[0].Trim()); # George Micahel R
Console.WriteLine(words[1].Trim()); # 412352434
Related
This question already has answers here:
get only 3 words of a String in C#
(2 answers)
Closed 4 years ago.
I have this string:
var a = "a new test string today";
How can I parse a to make another string that contains just the words
"a new test"
You could do this in a number of ways.
For example: Using Split, LINQ and Join
string.Join(" ", a.Split(' ').Take(3));
Or by finding the third space:
var firstSpace = a.IndexOf(' ');
var secondSpace = a.IndexOf(' ', firstSpace + 1);
var thirdSpace = a.IndexOf(' ', secondSpace + 1);
result = a.Substring(0, thirdSpace);
Error handling omitted.
This question already has answers here:
mask all digits except first 6 and last 4 digits of a string( length varies )
(13 answers)
Closed 4 years ago.
I'm using C# to create a pattern in order to replace all the occurrences in a string from
RX123456789 into RX*********
I have tried various patterns without success. I'm kinda new regarding Regular Expression.
I appreciate your help.
Use this Regex:-
string data = "RX123456789";
var resultString = Regex.Replace(data, #"[0-9]", "*");
If you need * of number only When RX is present then use this logic:-
string data = "RX123456789";
var resultString="";
if (new Regex("RX([0-9]+)").IsMatch(data))
{
resultString = Regex.Replace(data, #"[0-9]", "*");
}
This question already has answers here:
how to change nth element of the string
(5 answers)
Closed 5 years ago.
string input = "testeeeeeee";
char[] temp = input.ToCharArray();
var test = Regex.Replace(input.Replace(temp[1].ToString(), "#").Trim(), #"\s+", " ");
Console.WriteLine(test);
This is the code I have now, I want my second char on a string to be replaced with "#" , now is the problem that every e will be replaced in # , how to fix it that only the second char will be replaced and nothing more?
One way is to just assign a new value to the second char:
var input = "testeeeeeee".ToCharArray();
input[1] = '#';
var result = new string(input);
You'd want to do something like input[1] = # on the original string and not the char[] but as strings are immutable you cannot change it and the indexer is read-only.
Another way (which I think is less advisable):
var input = "testeeeeeee";
var result = input[0] + "#" + string.Concat(input.Skip(2));
For the second way it is cleaner to use SubString to get the string from the second index until the end
You can use the Substring function
string input = "testeeeeeee";
string new_input = input.Substring(0, 1) + "#" + input.Substring(2, input.Length)
This question already has answers here:
splitting a string based on multiple char delimiters
(7 answers)
Closed 5 years ago.
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string new = test.Trim("[");
I want output "AccoutNumber".
I have tried the below code but not getting the desired result:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[');
string newvalue = test[1];
Just use Split with two delimiters:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[', ']');
string newvalue = test[1];
You can also use Regex:
string test = "Account.Parameters[\"AccountNumber\"].Caption";
var match = System.Text.RegularExpressions.Regex.Match(test, ".*?\\.Parameters\\[\"(.*?)\"]");
if (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
}
.*? is a non greedy wildcart capture, so it will match your string until it reaches the next part (in our case, it will stop at .Parameters[", match the string, and then at "])
It will match .Parameters["..."]., and extract the "..." part.
you can do a Split to that string...
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string output = test.Split('[', ']')[1];
Console.WriteLine(output);
This question already has answers here:
RegEx Starts with [ and ending with ]
(4 answers)
Closed 6 years ago.
How to replace a set of characters where I only know the first and the last one, in between is a variable that is not constant.
All I know is that this string will always start with & and it will end with ;
string str = "Hello &145126451; mate!";
How to get rid of &145126451; ?
So the desired result is:
string result = "Hello mate!"
The most easiest way is to use Regex:
Regex yourRegex = new Regex(#"&.*;");
string result = yourRegex.Replace("Hello &145126451; mate!", String.Empty);
Console.WriteLine(result);
Here is a fiddle with example.