is there way to find out the indexof backslash in string variable?
i have string
var str = " \"AAP, FB, VOD, ART, BAG, CAT, DDL\"\n "
int stIdx = str.indexof('\"') // output as 6
int edIdx = str.indexof('\', stIdx+1); // output as -1
output i'm looking for is as below
AAP, FB, VOD, ART, BAG, CAT, DDL
You need to escape your backslash to use it as a char value.
Cause backslash is a special character, '\' didn't refer to backslash as a character, but as an instruction which let you escape any character.
int index = str.IndexOf('\\'); //should give you the right answer
A \" in a string is not a backslash followed by a quote, but just a quote. The backslash in the string literal escapes the quote: it should be just a character, not the end of the string constant.
The \n is not an escaped 'n' (it wouldn't need escaping) but a (single) newline character.
Your string literal doesn't really contain any backslashes.
Your code, with typos fixed:
var str = " \"AAP, FB, VOD, ART, BAG, CAT, DDL\"\n ";
int stIdx = str.IndexOf('"')+1; // output as 7 - you don't want position of " but 'A'
int edIdx = str.IndexOf('"', stIdx); // output as 39
Console.WriteLine($"from {stIdx} to {edIdx}"); // "from 7 to 39"
Console.WriteLine(str.Substring(stIdx, edIdx-stIdx)); // "AAP, FB, VOD, ART, BAG, CAT, DDL"
In a character literal (delimited by single quotes), you do not need to escape the double quote, so '"' works fine. Although it is no problem if you do escape it: '\"' - which is still a single " character.
The character sequence '\n' is used for a newline.
string str = "Hi\n";
In the above example str consists of the characters 'H', 'i', and a newline character. There is no '\' character.
Try the following:
int edIdx = str.indexof('\n', stIdx+1);
Related
I am trying to get the following Regex expressions into a single line.
filename = Regex.Replace(filename, #"[^a-z0-9\s-!/\-_\.\*\(\)']", ""); // Remove all non valid chars
filename = Regex.Replace(filename, #"\s+", " ").Trim(); // convert multiple spaces into one space
filename = Regex.Replace(filename, #"\s", "_"); // //Replace spaces by dashes
You can use
filename = Regex.Replace(filename, #"[^a-z0-9\s!/_.*()'-]|^\s+|\s+$|(\s+)", m => m.Groups[1].Success ? "_" : "");
The regex matches
[^a-z0-9\s!/_.*()'-]| - any char but lowercase ASCII letters, digits, whitespace, !, /, _, ., *, (, ), ' and -, or
^\s+| - start of string and then one or more whitespaces, or
\s+$| - one or more whitespaces and then end of string
(\s+) - Group 1: one or more whitespaces in any other context.
If Group 1 matches, the replacement is a _ char, else, the replacement is an empty string (the match is removed).
See the regex demo.
Replce single slash to double slash is not working always return the single slash..
string input;
input = "\r\t";
string mat1= input.Replace("\\\\","\\\\\\\\");
string inputt= mat1;
if i am run the above code it will return output is \r\t only....
but i need output like this
\r\t
"\r\t" is in fact just two characters, carriage return and tab. This is because the \ escape character is used to specify special characters.
If you want to have a string that is actually "\r\t" you need to escape the \ characters by using \\.
So your string should be:
input = "\\r\\t";
Or
input = #"\r\t";
And then to replace the backslashes with double backslashes:
string mat1= input.Replace("\\","\\\\");
Or
string mat1= input.Replace(#"\", #"\\");
input = "\r\t";
is a so called escaped string. \ means escape sequence. if you need \r\t you need to write
input = "\\r\\t";
This is 2 already escaped characters, not 4.
input="\r\t";
\r and \t are special literals. Check this article:
\r - Carriage return
\t - Horizontal tab
What you wanted, I gess, is to change this special literal like this:
string input;
input = "\r\t";
input = input.Replace("\r", "\\r");
input = input.Replace("\t", "\\t");
Console.WriteLine(input);
I have this bit of code, which is supposed to replace the Windows linebreak character (\r\n) with an empty character.
However, it does not seem to replace anything, as if I view the string after the regex expression is applied to it, the linebreak characters are still there.
private void SetLocationsAddressOrGPSLocation(Location location, string locationString)
{
//Regex check for the characters a-z|A-Z.
//Remove any \r\n characters (Windows Newline characters)
locationString = Regex.Replace(locationString, #"[\\r\\n]", "");
int test = Regex.Matches(locationString, #"[\\r\\n]").Count; //Curiously, this outputs 0
int characterCount = Regex.Matches(locationString,#"[a-zA-Z]").Count;
//If there were characters, set the location's address to the locationString
if (characterCount > 0)
{
location.address = locationString;
}
//Otherwise, set the location's coordinates to the locationString.
else
{
location.coordinates = locationString;
}
} //End void SetLocationsAddressOrGPSLocation()
You are using verbatim string literal, thus \\ is treated as a literal \.
So, your regex is actually matching \, r and n.
Use
locationString = Regex.Replace(locationString, #"[\r\n]+", "");
This [\r\n]+ pattern will make sure you will remove each and every \r and \n symbol, and you won't have to worry if you have a mix of newline characters in your file. (Sometimes, I have both \n and \r\n endings in text files).
How can I concatenate the string "\u" with "a string" to get "\u0000"?
My code creates two backslashes:
string a = #"\u" + "0000"; //ends up being "\\\u0000";
The escape sequence \uXXXX is part of the language's syntax and represents a single Unicode character. By contrast, #"\u" and "0000" are two different strings, with a total of six characters. Concatenating them won't magically turn them into a single Unicode escape.
If you're trying to convert a Unicode code point into a single-character string, do this:
char.ConvertFromUtf32(strUnicodeOfMiddleChar).ToString()
BTW, don't use == true; it's redundant.
If I understand you correctly, I think you want to build a single-char string from an arbitrary Unicode value (4 hex digits). So given the string "0000", you want to convert that into the string "\u0000", i.e., a string containing a single character.
I think this is what you want:
string f = "0000"; // Or whatever
int n = int.Parse(f, NumberStyles.AllowHexSpecifier);
string s = ((char) n).ToString();
The resulting string s is "\u0000", which you can then use for your search.
(With corrections suggested by Thomas Levesque.)
the line below creates tow backslash:
string a = #"\u" + "0000"; //a ends up being "\\u0000";
No, it doesn't; the debugger shows "\" as "\", because that's how you write a backslash in C# (when you don't prefix the string with #). If you print that string, you will see \u0000, not \\u0000.
Nope, that string really has single backslash in. Print it out to the console and you'll see that.
Escape your characters correctly!!
Both:
// I am an escaped '\'.
string a = "\\u" + "0000";
And:
// I am a literal string.
string a = #"\u" + "0000";
Will work just fine. But, and I am going out on a limb here, I am guessing that you are trying to escape a Unicode Character and Hex value so, to do that, you need:
// I am an escaped Unicode Sequence with a Hex value.
char a = '\uxxxx';
I have a function which replace character.
public static string Replace(string value)
{
value = Regex.Replace(value, "[\n\r\t]", " ");
return value;
}
value="abc\nbcd abcd abcd\ " if in string there is any unwanted white space they are also remove.Means I want result like this
value="abcabcdabcd".Help to change Regex Pattern to get desire result.Thanks a lot.
If you need to remove any number of whitespace characters from the string, probably you're looking for something like this:
value = Regex.Replace(value, #"\s+", "");
where \s matches any whitespace character and + means one or more times.
Instead of replacing your newline, tab, etc. characters with a space, just replace all whitespace with nothing:
public static string RemoveWhitespace(string value)
{
return Regex.Replace(value, "\\s", "");
}
\s is a special character group that matches all whitespace characters. (The backslash is doubled because the backslash has a special meaning in C# strings as well.) The following MSDN link contains the exact definition of that character group:
Character Classes: White-Space Character: \s
You may want to try \s indicating white spaces. With the statement Regex.Replace(value, #"\s", ""), the output will be "abcabcdabcd".