I have strings that look like this:
"\n 間違う\n "
It's really a \n and some spaces.
How can I remove these from the string?
Use Trim() which will remove any white-space (including new-lines, tabs, etc) from both left and right ends of a string:
var s = "\n 間違う\n ";
var trimmed = s.Trim();
// output
// 間違う
If you want to remove line-breaks between text (like 間\n違う\n) then you can use Replace:
var s = "間\n違う\n";
var trimmed = s.Trim().Replace(Environment.NewLine, string.Empty);
// output
// 間違う
Use String.Trim.
var s1 = "\n 間違う\n ";
var s2 = s1.Trim();
Debug.WriteLine(s2);
Related
I want to retrieve characters separated by a specific delimiter.
Example :
Here, I want to access the string between the " " delimiters. But I want the 2nd set of characters between "".
abc"def"ghi"jklm // Output : ghi
"hello" yes "world" // output : world
How can I get that?
I know we can use split. But sometimes the string might not start with " character.
Can anyone please help me with this?
You can just find the first quote, and use your approach from there:
var firstQuote = str.IndexOf('"');
var startsWithQuote = str.Substring(firstQuote);
string valueStr = "abc\"def\"ghi\"jklm";
var result = valueStr.Split('"')[2];
Console.WriteLine(result);
https://dotnetfiddle.net/T3fMof
Obviously check for the array elements before accessing them
You can use regular expressions to match them:
var test = "abc\"def\"ghi\"jklm";
var test2 = "\"hello\" yes \"world\"";
var match1 = Regex.Matches(test, ".+\"(.+)\"");
var match2 = Regex.Matches(test2, ".+\"(.+)\"");
Console.WriteLine("Match1: " + match1[0].Groups[1].Captures[0]);
Console.WriteLine("Match2: " + match2[0].Groups[1].Captures[0]);
// Match1: ghi
// Match2: world
I have source string
string source = "hemant \\\n test new line \\\t test tab";
want string using regular expression
string destination = "hemant test new line test tab"
(Here i just replace \n and \t with " " i.e white space)
So i had try
string destination = Regex.Replace(source, "[\\\n\\\\t]", " ");
this gives me destination = hema test ew line es ab "
Here it remove the \\n and \\t. But remove the nt from hemant n from new
string destination = Regex.Replace(source, "[\\n\\t]", " ");
this don't do anything
try this:
string destination = Regex.Replace(source, #"(?:\\n)|(?:\\t)", " ");
or even more simple:
string destination = Regex.Replace(source, #"\\[nt]", " ");
"[\\\n\\\\t]" is a class that describes \ n and t as individual characters so you lose all n & t characters.
Use: "\\\\\\n|\\\\\\t"
You can just use string.Replace for that.
destination = source.Replace("\\\n", "").Replace("\\\t", "");
But note that will leave two spaces between "hemat" and "test", as well as between "line" and "test". If you want to compress the spaces as well you can use this regular expression.
var destination = Regex.Replace(source, #"\s*(\\\n|\\\t)\s*", " ");
Notice the use of # so you don't have to double up on the backslashes.
Or assuming your string is actually
source = "hemant \\n test new line \\t test tab";
That is a backslash character followed by the letter "n" and a backslash followed by the letter "t". Then you could do something like this.
var destination = Regex.Replace(source, #"\s*\\[nt]\s*", " ");
Just use source.Replace("\\", " ").Replace("\n", " ").Replace("\t", " "); no need for regex here.
I want to remove tabs from a string. I am using this code but its not working.
string strWithTabs = "here is a string with a tab";
// tab-character
char tab = '\u0009';
String line = strWithTabs.Replace(tab.ToString(), "");
I tried this, but still its not working
String line = strWithTabs.Replace("\t", "");
It worked with String line = strWithTabs.Replace(" ", "");
But is their any other way to Identify tabs ?
I also looked at this post Removal of Tab-whitespace?
But it removed all the spaces from the string, where as I just want to remove Tabs.
Tab and space are not same, if tab is converted into spaces, replacing just "\t" will not work.
Below code will find tab and replace with single space and also find multiple spaces and replace it with single space.
string strWithTabs = "here is a string with a tab and with spaces";
string line = strWithTabs.Replace("\t", " ");
while(line.IndexOf(" ") >= 0)
{
line = line.Replace(" ", " ");
}
Edit: Since this is accepted, I'll amend it with the better solution posted by Emilio.NT which is to use Regex instead of while:
string strWithTabs = "here is a string with a tab and with spaces";
const string reduceMultiSpace= #"[ ]{2,}";
var line= Regex.Replace(strWithTabs.Replace("\t"," "), reduceMultiSpace, " ");
Because " " is not equal to tab character. \t is. It is an escape sequence character.
For example;
string strWithTabs = "here is a string\twith a tab";
char tab = '\u0009';
String line = strWithTabs.Replace(tab.ToString(), "");
line will be here is a stringwith a tab
You can't say a sentence like \t is equal to 6 spaces for example.
Use Regular Expression to reduce multiple spaces to one:
var strWithTabs = "here is a string with a tab and spaces";
const string reduceMultiSpace= #"[ ]{2,}";
var line= Regex.Replace(strWithTabs.Replace("\t"," "), reduceMultiSpace, " ");
Seems to be the compactest ...
//Eliminate tabs & multiple spaces => only 1 space between words
string input1 = " Hello World !";
Regex rgx2 = new Regex("\t|\\s+");
string result = rgx2.Replace(input1, " ");
Console.WriteLine("Original String: {0}", input1);
Console.WriteLine("Replacement String: {0}", result);
Console.ReadKey();
~~~
I have a lot of strings that look like this:
current affairs
and i want to make the string be :
current affairs
i try to use Trim() but it won't do the job
Regex can do the job
string_text = Regex.Replace(string_text, #"\s+", " ");
You can use regular expressions for this, see Regex.Replace:
var normalizedString = Regex.Replace(myString, " +", " ");
If you want all types of whitespace, use #"\s+" instead of " +" which just deals with spaces.
var normalizedString = Regex.Replace(myString, #"\s+", " ");
Use a regular expression.
yourString= Regex.Replace(yourString, #"\s+", " ");
You can use a regex:
public string RemoveMultipleSpaces(string s)
{
return Regex.Replace(value, #"\s+", " ");
}
After:
string s = "current affairs ";
s = RemoveMultipleSpaces(s);
Using Regex here is the way,
System.Text.RegularExpressions.Regex.Replace(input, #”\s+”, ” “);
This will removes all whitespace characters including tabs, newlines etc.
First you need to split the whole string and then apply trim to each item.
string [] words = text.Split(' ');
text="";
forearch(string s in words){
text+=s.Trim();
}
//text should be ok at this time
I am trying to remove " from a string using Regex.
I am receiving a string into a method, I would like to take the string and split it up into the words that are in the string.
My Code is below, hopefully you can see what I am doing.
The problem I am having is trying to tell Regex that " is what I would like to remove. I have tried numerous ways: I have searched Google for a answer and have had to resort to here.
search_string looks like this: blah="blah" la="la" ta="ta" and in the end I want just the blah blah la la ta ta.
public blahblah blahblah(blah blah, string search_string)
{
Regex r = new Regex(#"/"+");
string s3 = r.Replace(search_string, #" ");
Regex r2 = new Regex(" ");
Regex r3 = new Regex("=");
string[] new_Split = { };
string[] split_String = r2.Split(s3);
foreach (string match in split_String)
{
new_Split = r3.Split(match);
}
//do blahblah stuff with new_Split[1] .. etc
// new_Split[0] should be blah and new_Split[1] should
// be blah with out "", not "blah"
return blah_Found;
Just use:
myString = myString.Replace( "\"", String.Empty );
[Update]
The String.Empty or "" is not a space char. You wrote this
blah="blah" la="la" ta="ta"
you want to convert to
blah blah la la ta ta
So you have white spaces anyway. If you want this:
blahblahlalatata
you need to remove them too:
myString = myString.Replace( "\"", String.Empty ).Replace( " ", String.Empty );
for '=' do it again, and so on...
You need to be more precise in your questions.
As a quick thought - and barking maybe up entirely the wrong tree, but wouldnt you want something like
Regex r = new Regex("(\".*\")");
eg, a reg expression of ".*"
This is one way to do it.
It will Search for anything in that form: SomeWord="somethingelse"
and replace it with SomeWord somethingelse
var regex = new Regex(#"(\w+)=\""(.+)\""");
var result = regex.Replace("bla=\"bla\"", "$1 $2");
I can't help you with Regex.
Anyway if you only need to remove = and " and split words you could try:
string[] arr = s
.Replace("="," ")
.Replace("\""," ")
.Split(new string[1] {" "}, StringSplitOptions.RemoveEmptyEntries);
I did it in 2 passes
string input = "blah=\"blah\" la=\"la\" ta=\"ta\"";
//replace " and = with a space
string output = Regex.Replace(input, "[\"=]", " ");
//condense the spaces
output = Regex.Replace(output, #"\s+", " ");
EDIT:
Treating " and = differently as per comment.
string input = "blah=\"blah\" la=\"la\" ta=\"ta\"";
//replace " and = with a space
string output = Regex.Replace(input, "\"", String.Empty);
output = Regex.Replace(output, "=", " ");
Clearly regex is a bit overkill here.