2017-08-10 73 views
-6

的第一個字符在特定字符串,我要比較,如果字符串的第一個字符 爲「x」(任何),然後返回所有的字符串,否則空白,另一種情況是,如果有第四個字符爲「 - 「然後返回字符串找到字符串

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace @string 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string myString = "bhargav m patel".Trim(); 

      Console.Write("Type a char : "); 
      string mychar = Console.ReadLine(); 

      if (myString.StartsWith("[a-zA-Z]").Equals(mychar)) 
      { 
       Console.WriteLine("true"); 
      } 
      else 
      { 
       Console.WriteLine("false"); 
      } 

      Console.ReadLine(); 
     } 
    } 
} 
+0

您有什麼問題用這個代碼? – BACON

+0

我無法比較第一個字符。 –

+0

出了什麼問題'如果(myString.StartsWith(myChar))'? –

回答

0

那麼,我試了這個,它的工作perfeclty。它適合你的情況嗎?

static void Main(string[] args) 
{ 
    string myString = "bhargav m patel".Trim(); 

    Console.Write("Type a char : "); 
    string mychar = Console.ReadLine(); 

    if (myString.StartsWith(mychar)) 
     Console.WriteLine("true"); 
    else 
     Console.WriteLine("false"); 

    Console.ReadLine(); 
} 
0

林不知道我有ü要什麼,但: 如果要比較字符串第一個字符與字符的用戶有輸入,U可以這樣做:

if (myString.StartsWith(mychar)) 
     { 
      Console.WriteLine("true"); 
     }