i want to create a simple program that i have a array of strings and for each string i want to check if it contains specified character, i want to remove it.first of all i replace the specified characters with space and when i tried to trim the spaces it doesn't work
Here it is my code
char[] arr = new char[] {' '};
for (int i = 0; i < words.Length; i++)
{
words[i] = words[i].Replace('0', ' ');
words[i] = words[i].Trim(arr);
}
If you want to remove all spaces, instead of words[i] = words[i].Trim(arr);, you can use:
words[i] = words[i].Replace(" ", string.Empty);
Personally, I would do this for your first removal (0) as well:
words[i] = words[i].Replace("0", string.Empty); // Remove all "0" characters
words[i] = words[i].Replace(" ", string.Empty); // Remove all spaces
Or, even:
words[i] = words[i].Replace("0", string.Empty).Replace(" ", string.Empty);
Trim() only removes leading and trailing spaces. It won't remove spaces in the middle of a string. There's really no need to do all that work though. You can make a single call to Replace() by calling the appropriate overload:
for(int i = 0; i < words.Length; i++)
words[i] = words[i].Replace("0", "");
and for people who like one-liners:
words = words.Select(i => i.Replace("0", "").Replace(" ", "")).ToArray();
Related
const string Duom = "Text.txt";
char[] seperators = { ' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t' };
string[] lines = File.ReadAllLines(Duom, Encoding.GetEncoding(1257));
for (int i = 0; i < lines.Length; i++)
{
string GLine = " " + lines[i];
GLine = Regex.Replace(GLine, #"\s+", " ");
GLine = GLine.PadRight(5, ' ');
Console.WriteLine(GLine);
}
Reads a text file, for each line it adds a whitespace at the start, removes all double and above whitespaces, and I want to move the line to the right , but it doesn't do anything.
Result :
Expected Result:
PadLeft and PadRight doesn't add characters to the start/end of your string if the specified length has already been reached.
From the docs for String.PadRight (emphasis mine):
Returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.
All of your strings are larger than 5, the specified total length, so PadRight/PadLeft won't do anything.
"Padding" the string is adding spaces (or some other character) so that the new string is at least as large as the number you want.
Instead, just manually add 5 spaces before your string.
GLine = " " + GLine;
Or more programmaticly:
GLine = new string(' ', 5) + GLine;
You could replace the body of your loop like this:
string GLine = new string(' ', 1 + i * 5) + Regex.Replace(lines[i], #"\s+", " ");
Console.WriteLine(GLine);
This will add 1 space and then 5 more spaces for each line.
for (int i = 0; i < lines.Count(); i++)
{
string GLine = new string(' ',5*i) + lines[i];
Console.WriteLine(GLine);
}
This should add 5 extra spaces for each line you have, which i believe is what you are trying to accomplish if i understand correctly.
You need to left pad a tab depending on how many lines of text you have. The best increment to use is the i variable.
string GLine = " " + lines[i];
change this to
string GLine = new String('\t', i) + lines[i];
By the way, PadLeft should work but keep in mind you need to execute it i times
I want to count chars in a big text, I do it with this code:
string s = textBox.Text;
int chars = 0;
int words = 0;
foreach(var v in s.ToCharArray())
chars++;
foreach(var v in s.Split(' '))
words++;
this code works but it seems pretty slow with large text, so how can i improve this?
You don't need another char-array, you can use String.Length directly:
int chars = s.Length;
int words = s.Split().Length;
Side-note: if you call String.Split without an argument all white-space characters are used as delimiter. Those include spaces, tab-characters and new-line characters. This is not a complete list of possible word delimiters but it's better than " ".
You are also counting consecutive spaces as different "words". Use StringSplitOptions.RemoveEmptyEntries:
string[] wordSeparators = { "\r\n", "\n", ",", ".", "!", "?", ";", ":", " ", "-", "/", "\\", "[", "]", "(", ")", "<", ">", "#", "\"", "'" }; // this list is probably too extensive, tim.schmelter#myemail.com would count as 4 words, but it should give you an idea
string[] words = s.Split(wordSeparators, StringSplitOptions.RemoveEmptyEntries);
int wordCount = words.Length;
You can do this in a single pass through without making a copy of your string:
int chars = 0;
int words = 0;
//keep track of spaces so as to only count nonspace-space-nonspace transitions
//it is initialized to true to count the first word only when we come to it
bool lastCharWasSpace = true;
foreach (var c in s)
{
chars++;
if (c == ' ')
{
lastCharWasSpace = true;
}
else if (lastCharWasSpace)
{
words++;
lastCharWasSpace = false;
}
}
Note the reason I do not use string.Split here is that it does a bunch of string copies under the hood to return the resulting array. Since you're not using the contents but instead are only interested in the count, this is a waste of time and memory - especially if you have a big enough text that has to be shuffled off to main memory, or worse yet swap space.
Do be aware that string.Split does on the other hand by default use a longer list of delimiters than just ' ', so you may want to add other conditions to the if statement.
You can simply use
int numberOfLetters = textBox.Length;
or use LINQ
int numberOfLetters = textBox.ToCharArray().Count();
or
int numberOfLetters = 0;
foreach (char letter in textBox)
{
numberOfLetters++;
}
var chars = textBox.Text.Length;
var words = textbox.Text.Count(c => c == ' ') + 1;
At the next code I'm splitting text to words, inserting them into a table separately and counting the numbers of letters in each word.
The problem is that counter is also counting spaces at the beginning of each line, and give me wrong value for some of the words.
How can I count only the letters of each word exactly?
var str = reader1.ReadToEnd();
char[] separators = new char[] {' ', ',', '/', '?'}; //Clean punctuation from copying
var words = str.Split(separators, StringSplitOptions.RemoveEmptyEntries).ToArray(); //Insert all the song words into "words" string
string constring1 = "datasource=localhost;port=3306;username=root;password=123";
using (var conDataBase1 = new MySqlConnection(constring1))
{
conDataBase1.Open();
for (int i = 0; i < words.Length; i++)
{
int numberOfLetters = words[i].ToCharArray().Length; //Calculate the numbers of letters in each word
var songtext = "insert into myproject.words (word_text,word_length) values('" + words[i] + "','" + numberOfLetters + "');"; //Insert words list and length into words table
MySqlCommand cmdDataBase1 = new MySqlCommand(songtext, conDataBase1);
try
{
cmdDataBase1.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
This will be a simple and fast way of doing so:
int numberOfLetters = words[i].Count(word => !Char.IsWhiteSpace(word));
Another simple solution that will save you the above and rest of the answers here, will be to Trim() first, and than do your normal calculation, due your statement that it is happening just in the beginning of every line.
var words = str.Trim().Split(separators, StringSplitOptions.RemoveEmptyEntries);
Than all you will need is: (Without the redundant conversion)
int numberOfLetters = words[i].Length;
See String.Trim()
int numberOfLetters = words[i].Trim().ToCharArray().Length; //Calculate the numbers of letters in each word
instead of ' ' use '\s+' since it matches one or more whitespace at once, so it splits on any number of whitespace characters.
Regex.Split(myString, #"\s+");
string str = "ghy saste mater"
How do I replace first letter in each word from str to the letter "w"?
So the new string would be:
"why waste water"
How can I achieve this?
You would explode string.Split() your string using the delimeter of " " this would give you an array of strings and then you can loop over the entire array and call:
string.Replace(string[0], 'w')
A far more impressive manner of doing this would be using Regex.Replace() and writing a Regex to find the combination of a space and a character and then replace that string with a space and a w.
something like " a" would be replaced with " w"
This is really straitforward so I only provided some hints. Look-up what I talked about here and you'll be fine.
Try this :
string str = "ghy saste mater";
string[] parts = str.Split(' ');
string result = string.Join(" ", parts.Select(p => "w" + (p.Length > 1 ? p.Substring(1, p.Length - 1) : "")).ToArray());
You could split the string into an array and then change the first character in each element of the new array.
Something like:
string[] str_array = str.Split(' ');
for (int i = 0; i < str_array.Length; i++) {
str_array[i] = "w" + str_array[i].Substring(1, str_array[i].Length);
}
Then you would have to turn the array back into a string.
A neat way is to use Regex.Replace():
string str = "ghy saste mater";
var strModified = Regex.Replace(str, #"\b\w", "w");
You also could use LINQ:
var str = "ghy saste mater"
var strModified = string.Join(" ", str.Split().Select(s => "w" + s.Substring(1, s.Length - 1))));
If you modify strings a lot, consider using StringBuilder class:
var str = "ghy saste mater"
var strModified = str.Split().Select(s => "w" + s.Substring(1, s.Length - 1)).
Aggregate(new StringBuilder(), (b, s) => b.Append(s + " ")).ToString();
Just iterate through it.
string o = "";
o += str[0]
for (int i = 1; i < str.length; i++){
o+=str[i-1] == ' ' ? 'w' : str[i];
}
I use Visual Studio 2010 ver.
I have array strings [] = { "eat and go"};
I display it with foreach
I wanna convert strings like this : EAT and GO
Here my code:
Console.Write( myString.First().ToString().ToUpper() + String.Join("",myString].Skip(1)).ToLower()+ "\n");
But the output is : Eat and go . :D lol
Could you help me? I would appreciate it. Thanks
While .ToUpper() will convert a string to its upper case equivalent, calling .First() on a string object actually returns the first element of the string (since it's effectively a char[] under the hood). First() is actually exposed as a LINQ extension method and works on any collection type.
As with many string handling functions, there are a number of ways to handle it, and this is my approach. Obviously you'll need to validate value to ensure it's being given a long enough string.
using System.Text;
public string CapitalizeFirstAndLast(string value)
{
string[] words = value.Split(' '); // break into individual words
StringBuilder result = new StringBuilder();
// Add the first word capitalized
result.Append(words[0].ToUpper());
// Add everything else
for (int i = 1; i < words.Length - 1; i++)
result.Append(words[i]);
// Add the last word capitalized
result.Append(words[words.Length - 1].ToUpper());
return result.ToString();
}
If it's always gonna be a 3 words string, the you can simply do it like this:
string[] mystring = {"eat and go", "fast and slow"};
foreach (var s in mystring)
{
string[] toUpperLower = s.Split(' ');
Console.Write(toUpperLower.First().ToUpper() + " " + toUpperLower[1].ToLower() +" " + toUpperLower.Last().ToUpper());
}
If you want to continuously alternate, you can do the following:
private static string alternateCase( string phrase )
{
String[] words = phrase.split(" ");
StringBuilder builder = new StringBuilder();
//create a flag that keeps track of the case change
book upperToggle = true;
//loops through the words
for(into i = 0; i < words.length; i++)
{
if(upperToggle)
//converts to upper if flag is true
words[i] = words[i].ToUpper();
else
//converts to lower if flag is false
words[i] = words[i].ToLower();
upperToggle = !upperToggle;
//adds the words to the string builder
builder.append(words[i]);
}
//returns the new string
return builder.ToString();
}
Quickie using ScriptCS:
scriptcs (ctrl-c to exit)
> var input = "Eat and go";
> var words = input.Split(' ');
> var result = string.Join(" ", words.Select((s, i) => i % 2 == 0 ? s.ToUpperInvariant() : s.ToLowerInvariant()));
> result
"EAT and GO"