Reputation: 81
I am new to programming. I need to determine if a given string begins with something. For example to check if the string begins with "hi" to return true, but to return false if it's "high".
StartHi("hi there") -> true;
StartHi("hi") -> true;
StartHi("high five") -> false.
I've tried with .Substring and .StartsWith, but i can't figure out how to make them return false "high five". I've tried the like this:
public static bool StartHi(string str)
{
bool firstHi;
if(string.IsNullOrEmpty(str))
{
Console.WriteLine("The string is empty!");
}
else if(str.Substring(0,2) == "hi")
{
firstHi = true;
Console.WriteLine("The string starts with \"hi\"");
}
else
{
firstHi = false;
Console.WriteLine("The string doesn't start with \"hi\"");
}
Console.ReadLine();
return firstHi;
} With .StartsWith, just changed the "else if":
else if(str.StartsWith("hi"))
{
firstHi = true;
Console.WriteLine("The string starts with \"hi\"");
}
Thank You in advance!
Upvotes: 7
Views: 13770
Reputation: 30464
I guess you want to return true if your string starts with the WORD high. This means that you want a non-word character after the h and the i. Non-word characters are called white-spaces (tabs, commas, semicolons, line feeds and a lot other ones.
You can use StartsWith to find if it starts with "hi". To check if the the character after the hi is a whitespace use the static function String.IsNullOrWiteSpace(character after hi) To get to the character after hi: use String.Remove.
Other problems that you'll have to face: What to do with upper case and lower case: do you define the following as true?
Upvotes: 0
Reputation: 15112
Write your StartHi
method like below using Split
public static bool StartHi(string str)
{
bool firstHi = false;
if (string.IsNullOrEmpty(str))
{
Console.WriteLine("The string is empty!");
Console.ReadLine();
return false;
}
var array = str.Split(new string[] {" "}, StringSplitOptions.None);
if (array[0].ToLower() == "hi")
{
firstHi = true;
Console.WriteLine("The string starts with \"hi\"");
}
else
{
firstHi = false;
Console.WriteLine("The string doesn't start with \"hi\"");
}
Console.ReadLine();
return firstHi;
}
Note:
If you have other strings like "hi!" or "Hi? Don't say hi to me", then you can extend the Split
to something like below.
var array = str.Split(new string[] {" ", "!", "?"}, StringSplitOptions.None);
if (array[0].ToLower() == "hi") //convert to lower and check
Regex is probably your best bet if it gets more complicated and look towards it. I can't give it one since I'm not great with it.
Upvotes: 1
Reputation: 3718
Two ways come to mind to achieve this. The first would be to split the string on whitespace, into an array, then check the first entry of the array for "hi":
string[] words = str.split(' ');
if ((words.length == 0 && str == "hi") || (words[0] == "hi"))
return true;
else
return false;
The second would be to utilise the Regex and check if it matches "hi" at the start of the string:
return (System.Text.RegularExpressions.Regex.Match(str, @"^hi\b").Success);
Both of these will only find "hi" however (case specific). If you wish to check for "hi", "Hi", "HI" or "Hi", then you would likely want to use the ".ToLower()" method on the string object:
string lowerStr = str.ToLower();
string[] words = lowerStr.split(' ');
if ((words.length == 0 && lowerStr == "hi") || (words[0] == "hi"))
return true;
else
return false;
An example of your StartHi method may look like this:
public static bool StartHi(string str)
{
bool firstHi;
if(string.IsNullOrEmpty(str))
{
Console.WriteLine("The string is empty!");
}
else
{
string strLower = str.ToLower();
string[] words = strLower.split(' ');
if ((words.length == 0 && strLower == "hi") || (words[0] == "hi"))
{
firstHi = true;
Console.WriteLine("The string starts with \"hi\"");
}
else
{
firstHi = false;
Console.WriteLine("The string doesn't start with \"hi\"");
}
}
Console.ReadLine();
return firstHi;
}
If you needed to expand your criteria, and treat examples like "Hi!" and "Hi?" as a success, you should lean towards the Regex method. In which case, your method may look like the following:
public static bool StartHi(string str)
{
bool firstHi;
if(string.IsNullOrEmpty(str))
{
Console.WriteLine("The string is empty!");
}
else if (System.Text.RegularExpressions.Regex.Match(str, @"^hi\b").Success))
{
firstHi = true;
Console.WriteLine("The string starts with \"hi\"");
}
else
{
firstHi = false;
Console.WriteLine("The string doesn't start with \"hi\"");
}
Console.ReadLine();
return firstHi;
}
Upvotes: 2