Convert text entered by user containing with escape sequence to string [duplicate] - c#

This question already has answers here:
Replacing all the '\' chars to '/' with C#
(7 answers)
Closed 6 years ago.
In textbox1 user enters string "Test\u0021Test" and I would like to convert escaped character "\u0021" to "!"
string x = "Test\u0021Test"; // this is easy
string y = textbox1.Text; // here textbox1.Text = "Test\u0021Test" and this I don't know how to convert
Thanks for help
EDIT
Answered by #Simo Erkinheimo
Allow user to enter escape characters in a TextBox

Use string Replace method this case
string x = "Test\u0021Test"; // this is easy
string y = textbox1.Text.Replace("\u0021", "!");

You can do something like this. It will work for all unicode escaped symbols in input string.
var result = Regex.Replace(x, #"\\[u]([0-9a-f]{4})",
match => char.ToString(
(char)ushort.Parse(match.Groups[1].Value, NumberStyles.AllowHexSpecifier)));

Related

How to split a string array using char as delimiter [duplicate]

This question already has answers here:
How to split a string by a specific character? [duplicate]
(6 answers)
Closed 10 months ago.
I want to add ' char as delimiter to my string array but I have error telling conversion string to char impossible.
I see this How to split a string by a specific character?
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split("'").ToString();
The issues is that you have the .ToString() after your statement.
Change it to this:
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split('\'');
The reason why this code doesn't work:
string[] splitTest = ineedtosplitthis.Split("'").ToString();
Is because you are converting it to a string via ToString();, while assigning it to a string[].
Split(); already returns a string[], so there is no need to convert it.
Hope this explanation helped :)

get the characters before and after certain character having variable length [duplicate]

This question already has answers here:
Split string and get Second value only
(5 answers)
Closed 3 years ago.
I am trying to get the characters that appear before and after certain character ("-").
string val = "7896-2-5";
7896-2-5 here I want to get the character that appear between the two dashes i.e. 2
string val = "4512-12-5";
4512-12-5 so here 12,
the position of first appearance of - is fixed from left side but the position of second appearance of - is determined by the character in between the two - , may be single digit or double digit number.
How can I get the characters?
Easiest would be to use string.Split('-')
e.g.
var middleDigit = string.Split('-')[1];
if your string should have tow dashes try this:
string myString = "4512-12-5";
string result="";
if(myString.Count(f => f =='-') == 2)
result = myString.Substring(myString.IndexOf('-') + 1 ,myString.LastIndexOf('-') - myString.IndexOf('-') - 1);
else
result = "string is not well formated";
Console.WriteLine(result);

Extract a character after a particular string [duplicate]

This question already has answers here:
Regex for numbers after a certain string part
(2 answers)
Closed 4 years ago.
I want to extract a character after a particular string, example:
Sentence: "Develop1 Tester2 BA3"
String: Develop
Expected result: "1"
I tried the Regex as following but still not get result as my expectation, please consult me, thank in advance.
RegEx: /[DEVELOP\d]\[+-]?\d+(?:\.\d*)?/
You don't really need a regex for this.
Something like this should do the trick:
var input = "Develop1 Tester2 BA3";
var search = "Develop";
if (input.StartsWith(search) && input.Length > search.Length) {
var result = input[search.Length];
Console.Write("Result: " + result);
}

Replace a set of characters in a string [duplicate]

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.

Extract part of the string [duplicate]

This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 7 years ago.
I have thousands of lines of string type data, and what I need is to extract the string after AS. For example this line:
CASE END AS NoHearing,
What I want would be NoHearing,
This line:
CASE 19083812 END AS NoRequset
What I need would be NoRequset
So far I have tried couple ways of doing it but no success. Can't use .split because AS is not Char type.
If this will be the only way that AS appears in the string:
noRequestString = myString.Substring(myString.IndexOf("AS") + 3);
Using Regex I extract all between the AS and a comma:
string data = #"
CASE END AS NoHearing,
CASE 19083812 END AS NoRequset
";
var items = Regex.Matches(data, #"(?:AS\s+)(?<AsWhat>[^\s,]+)")
.OfType<Match>()
.Select (mt => mt.Groups["AsWhat"])
.ToList();
Console.WriteLine (string.Join(" ", items)); // NoHearing NoRequset

Categories