2016-04-03 11 views
0

フルネームの文字列を2つに分岐して最初と最後の名前を別々に出力するタスクが与えられました。たとえば、入力:Steve Robertson、出力:First name: SteveLast name: Robertsonsplit関数を使用せずにフルネーム文字列を別々の変数(first、middle、last)に分割する

私は成功しました。かなり簡単でした。しかし、私はフルネーム文字列を最初に、最後に中間に分割することに問題があります。これまで私が行ってきたことがあります。

Sub Main() 
     Dim string1 As String = "" 
     Dim string2 As String = "" 
     Dim string3 As String = "" 
     Dim string4 As String = "" 
     Dim temp1 As String = "" 
     Dim integer1 As Integer = 1 
     Console.WriteLine("Enter the string you want to bifurcate: ") 
     string1 = Console.ReadLine() 
     While integer1 <> 0 
      integer1 = InStr(string1, " ") 
      Console.WriteLine(string1) 
      string2 = Left(string1, integer1) 
      Console.WriteLine(string2) 
      string3 = Mid(string1, integer1 + 1) 
      Console.WriteLine(string3) 
      string4 = Mid(string1, integer1 + 1) 
      Console.WriteLine(string4) 
      string1 = string4 
     End While 
     Console.WriteLine("First name is: " & string2) 
     Console.WriteLine("Second name is: " & string3) 
     Console.WriteLine("Third name is: " & string4) 
     Console.ReadKey() 
    End Sub 

私は彼らの値は反復中にあるものを見るために、ほぼすべての単一の変数を印刷してることに注意してください。私はlen()関数と既にコード内にあるものだけを使うことができます。

EDIT:

だから私は周りいじっそして最後のものを持って、ループのない、しかし、私は、変数を繰り返すことなくこれを行うためのクリーナー/正しい方法があったか疑問に思うし、また、任意に作成する必要はなかったです新しいものも。

たぶん
Sub Main() 
    Dim string1 As String = "" 
    Dim string2 As String = "" 
    Dim string3 As String = "" 
    Dim string4 As String = "" 
    Dim integer1 As Integer 
    Console.WriteLine("Enter the string you want to split: ") 
    string1 = Console.ReadLine() 
    integer1 = InStr(string1, " ") 
    string2 = Left(string1, integer1) 
    string3 = Mid(string1, integer1 + 1) 
    integer1 = InStr(string3, " ") 
    string4 = Left(string3, integer1) 
    string3 = Mid(string3, integer1 + 1) 
    Console.WriteLine("The first name is: " & string2) 
    Console.WriteLine("The middle name is: " & string4) 
    Console.WriteLine("The last name is: " & string3) 
    Console.ReadKey() 
End Sub 
+0

一度名前を入力してください。また、どうやってユーザーの入力を検証するのか、それとも重要なのでしょうか?現在の方法は多くのことですが、これはほんの数行で行うことができます...また、あなたが望むことを何でもしたり、クラスや関数を作成したりできますか? – Codexer

+0

私は分かりません。はい、あなたはそれを一度だけ入力することができます。これは全体の名前であるはずです。スティーブ・アール・ロバートソン。 – Nick

+0

素晴らしいですが、独自の関数、クラスなどを作成できますか?また、ユーザーが唯一の文字列を入力した場合、検証はどうですか? – Codexer

答えて

0

私はあなたが最初と最後の名前

Module Module1 

<Extension()> 
Public Function returnFirst(ByVal fullName As String) As String 

    Return fullName.Substring(0, fullName.IndexOf(" ")) 
End Function 

<Extension()> 

Public Function returnLast(ByVal fullName As String) As String 

    Return fullName.Substring(fullName.IndexOf(" ") + 1) 

End Function 

End Module 

を返します拡張メソッドを設計することを示唆している。この

Dim fullName = "Juan Perez" 
Dim name = fullName.Substring(0, fullName.IndexOf(" ")) 
Dim lastName = fullName.Substring(fullName.IndexOf(" ") + 1) 

How to separate full name string

のようなものは、「

それを呼び出します
'import module1 

Dim Name as string = 'FirstName LastName' 

MessageBox.Show(NAME.returnFirst) 
MessageBox.Show(NAME.returnLast) 
+0

IndexOfとは何ですか? – Nick

+1

@Nick https://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx – Dejan

+0

@ dejan87を見てください。彼はメソッドやクラスなどを作成できないと言っています... – Codexer

2

これを行う方法が1つあります。ユーザーの入力から文字をループします。それらを一緒に連結して、List(Of String)に投げ込んで、最後に簡単に書き出すことができます...このアカウントは、名前の間に複数のスペースがある場合も複数のスペースがあります。また、私はコードにいくつかのコメントを入れて、理解しやすくすることができます。

注:これはそれを行うための唯一の方法...である(他の方法がある)TRIED

コードやプログラム

Dim nList As New List(Of String) 
    Dim uStr As String = String.Empty 

    Console.WriteLine("Enter the string you want to bifurcate: ") 
    uStr = Console.ReadLine() 
    If Not String.IsNullOrEmpty(uStr) Then 'Make sure something was entered... 
     Dim tStr As String = String.Empty 

     'Loop through user's input... 
     Do Until uStr = String.Empty 
      For Each c As Char In uStr 
       If Not Char.IsWhiteSpace(c) Then 'If it's a space we can add to the current string... 
        tStr &= c.ToString 
       Else 
        'We can assume its another section of the name... 
        Exit For 
       End If 
      Next 
      'If its a space, remove it from current string... 
      If String.IsNullOrEmpty(tStr) Then uStr = uStr.Remove(0, tStr.Length + 1) : Continue Do 
      'Add the string to the list, could be first name, middle or lastname? 
      If nList.Count = 0 Then 
       nList.Add("First Name: " & tStr) 
      ElseIf nList.Count = 1 Then 
       nList.Add("Middle Name: " & tStr) 
      ElseIf nList.Count = 2 Then 
       nList.Add("Last Name: " & tStr) 
      End If 

      'Now we can remove what we got from the users input... 
      uStr = uStr.Remove(0, tStr.Length) 
      tStr = String.Empty 
     Loop 

    End If 

    'Finally write out the values... 
    Console.WriteLine(String.Join(Environment.NewLine, nList.ToArray)) 
    Console.ReadLine() 

スクリーンショットをテスト

enter image description here

+0

本当にありがとうと思います。私は1つの質問がある、文字列の 'リスト'は基本的に配列ですか? – Nick

+0

はい文字列の配列です。あなたの歓迎も! – Codexer

関連する問題