Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am unable to remove a tab from a sentence. Please check my code for more details.

Thank you in advance.

What I have tried:

C#
static void Main(string[] args)
        {

            String str1 = "Great punctuation: keep only words, skip all punctuation.\tAlso - skip all spaces and tab characters!";

            char[] spearator = { ',', ' ', '!', ':', ';', '.', '-'};
            
            String[] strlist1 = str1.Split(spearator,
               StringSplitOptions.RemoveEmptyEntries);

            foreach (String x in strlist1)
            {
                Console.WriteLine(x);
            }

        }

Output:

Great
punctuation
keep
only
words
skip
all
punctuation
        Also // How Do I remove tab too? I can't add '/t' - it does not work
skip
all
spaces
and
tab
characters

I need the output to be:

Great
punctuation
keep
only
words
skip
all
punctuation
Also // Without Tab
skip
all
spaces
and
tab
characters
Posted
Updated 8-Feb-22 10:03am
v3
Comments
Richard MacCutchan 8-Feb-22 7:08am    
Use '\t' for tab.

The value '/t' isn't a valid character code. A single tab character is represented by '\t' instead. You need to use a back-slash rather than a forward-slash.
C#
char[] spearator = { ',', ' ', '!', ':', ';', '.', '-', '\t'};
 
Share this answer
 
Comments
LuckyChloe 8-Feb-22 6:58am    
Thank You
You have escaped the tab incorrectly using '/t'.

char[] spearator = { ',', ' ', '!', ':', ';', '.', '-', '\t' };
 
Share this answer
 
Comments
LuckyChloe 8-Feb-22 6:59am    
Thank You
You can use Replace method to replace '\t' with empty string. Like:
String str1 = "Great punctuation: keep only words, skip all punctuation.\tAlso - skip all spaces and tab characters!";
str1 = str1.Replace("\t", "");
char[] spearator = { ',', ' ', '!', ':', ';', '.', '-' };
String[] strlist1 = str1.Split(spearator, StringSplitOptions.RemoveEmptyEntries);
foreach (String x in strlist1)
{
    Console.WriteLine(x);
}
Output:
Great
punctuation
keep
only
words
skip
all
punctuation
Also
skip
all
spaces
and
tab
characters
 
Share this answer
 
Comments
LuckyChloe 8-Feb-22 7:02am    
That was interesting solution.
Thank you
M Imran Ansari 8-Feb-22 7:06am    
Pleasure. Happy Coding!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900