7

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!

8
  • 5
    use regex @"^hi\b" Commented Jul 23, 2015 at 13:25
  • 3
    hi and high clearly begin with the same first two characters, therefore .Substring(0,2) and .StartsWith("hi") would always return true Commented Jul 23, 2015 at 13:28
  • 1
    Does Hi, pass your req Commented Jul 23, 2015 at 13:31
  • 3
    Surely you could just check for str.StartsWith("hi ") ? Commented Jul 23, 2015 at 13:33
  • 1
    @Sk93 ah of course, it depends what OP needs the check for I guess. Commented Jul 23, 2015 at 13:37

3 Answers 3

2

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; } 
Sign up to request clarification or add additional context in comments.

2 Comments

What about Hi!? Or Hi, anybody there?. In this cases, spliting on whitespace does not work (the regex solution would).
For the regex solution, you could also use RegexOptions.IgnoreCase (if that's what OP wants, I dunno).
1

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.

3 Comments

this will throw an error if the string is null. It also doesn't accomodate for upper/lower case, and it won't work for "hi!" or similar punctuation.
@Sk93 - this won't throw an error when string is null and this is as per OP's possible strings in the question. Although point noted, I will update my answer
@Sk93 - I see what you're saying about it being null.. it needed a return in the first loop.
0

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?

  • HI
  • hI

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.