Thursday, June 19, 2014
String_Tutorial
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions; //Add to work with Regex
namespace Zau_String_Class_Lab_Assignment
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Finding 2nd white space position
private void btnIndexOfSecondSpace_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
string inputstr = txtInputStr.Text;
//get index of first whitespace
char whitespace = ' ';
int index1 = inputstr.IndexOf(whitespace);
//get index of second whitespace
int index2 = inputstr.IndexOf(whitespace, index1 + 1);
richTextBox1.Text =
"Index of second whitespace is: " + index2;
//===================Different way to do==============
//string inputStr = txtInputStr.Text;
//string regex = @"(\s)";
//Match match = Regex.Match(inputStr, regex);
//if (match.Success)
//{
// match = match.NextMatch();
// richTextBox1.Text = "Index of 2nd position is: " + match.Index;
//}
//else
//{
// richTextBox1.Text = "No 2nd white space position was found.";
//}
}
//Count words in string
private void btnCountWordInString_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
string inputStr = txtInputStr.Text;
char[] seperator = {' '};
string[] words = inputStr.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
richTextBox1.Text = "This string contains " + words.Length.ToString() + " words";
}
private void btnSwapFirstAndLastWord_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
if (txtInputStr.Text != String.Empty)
{
string inputStr = txtInputStr.Text;
char[] seperator = {' '};
string[] words = inputStr.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
string tempWord = words[0];
words[0] = words[words.Length - 1];
words[words.Length - 1] = tempWord;
foreach (string word in words)
{
richTextBox1.AppendText(word + " ");
}
}
}
private void btnInsertWordIn2ndLast_Click(object sender, EventArgs e)
{
///Insert new word before the last word in the input string
string inputstr = txtInputStr.Text;
if (inputstr != String.Empty)
{
//get index of first whitespace
char whitespace = ' ';
//get word to insert
string word = txtSubstr.Text;
//=========================use the insert method in the String class=====
//get index of the last whitespace
int index = inputstr.LastIndexOf(whitespace);
if (index > 0)
{
inputstr = inputstr.Insert(index + 1, word + " ");
}
else
{
//the inputstr has one word only, insert at index:0
inputstr = inputstr.Insert(0, word + " ");
}
txtInputStr.Text = inputstr;
}
//=================different way to do=============================
//if (txtInputStr.Text != String.Empty && txtSubstr.Text != String.Empty)
//{
// string inputStr = txtInputStr.Text.Trim();
// string additionalString = txtSubstr.Text.Trim();
// char[] seperator = {' '};
// List<string> wordsList = inputStr.Split(seperator, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
// wordsList.Insert(wordsList.Count - 1, additionalString);
// for (int i = 0; i < wordsList.Count; i++)
// {
// richTextBox1.AppendText(wordsList[i] + " ");
// }
//}
//else
//{
// MessageBox.Show("Both The Input TextBox And The Text To Insert TextBox Must Be Populated.");
//}
//txtSubstr.Clear();
}
private void btnSearch_Click(object sender, EventArgs e)
{
if (txtSearchName.Text.Length == 1)
{
char inputChar = Convert.ToChar(txtSearchName.Text);
string[] names = { "Alex", "Anna", "Bawk", "Benforster", "Charle", "Dan", "Deck", "Zau" };
foreach (string name in names)
{
if (name[0] == inputChar)
richTextBox1.AppendText(name + "\n");
}
}
}
private void btnEnterString_Click(object sender, EventArgs e)
{
string inputstr = txtInputStr.Text;
richTextBox1.AppendText (inputstr);
txtInputStr.Clear();
}
//Text replace
private void btnWordToChange_Click(object sender, EventArgs e)
{
string strLine = richTextBox1.Text;
if (strLine != string.Empty)
{
string Word2Change = txtWordToChange.Text;
string newWord = txtNewWord.Text;
string correctString = strLine.Replace(Word2Change, newWord);
richTextBox1.AppendText("\n" + correctString);
}
}
}
}
//=====================LAB ASSINGMENT FOR TUESDAY 6/17===============
//Create a new project where user can enter main string
//Determine the index of the second whitespace
//Determine the number of words in the main string
//Swap the first and last words
//Insert a new word (or substring) right before the last word of the main string.
//Create an array of string. Populate it with names
//Use a foreach that displays only the names that start
//with a some character. (This character is read from user).
/// =====================6/18/2014====================
/// provide a way to replace any word by a new word
/// Ask user which word to replace
/// Get the new word
/// replace it
/// Display outcome
Labels:
C#
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment