使用字符串方法解析字符串

时间:2015-09-15 04:11:48

标签: string parsing

我在使用这个程序时遇到了麻烦。目标是使用字符串方法解析名称(作为字符串)。我只能得到名字和姓,但是我遇到了前缀问题。这是我到目前为止所有的帮助表示赞赏!

4 个答案:

答案 0 :(得分:2)

我的理解是全名可以包含Initials,FirstName,MiddleName和Last Name。在从全名中提取名称时,可能会有一些组合可以小心。

第一个案例

string fullName = "Mohit Shrivastava";
var names = fullName.Split(' ');
string firstName = names[0];
string lastName = names[1];

第二种情况

string fullName = "Mr. Mohit Shrivastava";
var names = fullName.Split(' ');
string prefix= names[0];
string firstName = names[1];
string lastName = names[2];

第三种情况

string fullName = "Mr. Mohit Kumar Shrivastava";
var names = fullName.Split(' ');
string prefix= names[0];
string firstName = names[1];
string middleName = names[2];
string lastName = names[3];

在您的情况下,Case Second将满足您的需求。如果您不确定用户将其名称作为他/她的名字,那么您可以计算名称数组中的项目,并查看哪种情况适合您。

答案 1 :(得分:0)

这样就可以了解

var parts = fullName.Replace("  ", " ").Trim().Split(' ');
lastName = parts[parts.Length - 1];
if (parts.Length >= 2)
   firstName = parts[parts.Length - 2];
if (parts.Length >= 3)
   prefix = parts[parts.Length - 3];

答案 2 :(得分:0)

如果您必须假设名称将是前缀+空格+名字+空格+姓氏或名字+空格+姓氏,那么您是否只能使用分割功能?如下(未经测试):

function companyFormValidation()
    {
        var name    = document.getElementById('companyname').value;
        var title   = document.getElementById('companytitle').value;
        var desc    = document.getElementById('description').value;
        var logo    = document.getElementById('logo').value;
        var email   = document.getElementById('emailid').value;
        var website = document.getElementById('siteurl').value;
        var phonenumber = document.getElementById('phonenumber').value;
        var faxNumber   = document.getElementById('faxNumber').value;
        var address     = document.getElementById('address').value;
        var latitude    = document.getElementById('latitude').value;
        var longitude   = document.getElementById('longitude').value;

        if(name == '')
        {
            alert('Name can not empty');
            //$("#ename").html("Name can't be empty..");

        }
    }



<form id="addCompanyForm" method="post" action="#" class="form-horizontal" >
                    <fieldset>
                        <legend>Company </legend>
                            <div class="form-group">
                                <label class="col-lg-3 control-label">Name</label>
                                <div class="col-lg-5">
                                    <input type="text" class="form-control" name="companyname" id='companyname' />
                                    <div id="ename"></div>
                                </div>

                            </div>

                            <div class="form-group">
                                <label class="col-lg-3 control-label">Title</label>
                                <div class="col-lg-5">
                                    <input type="text" class="form-control" name="companytitle" id="companytitle" />
                                    <div id="etitle"></div>
                                </div>

                            </div>
                            <div class="form-group">
                                <label class="col-lg-3 control-label">Description</label>
                                <div class="col-lg-5">
                                <textarea class="form-control" name="description" id = "description"></textarea>
                                <div id="edesc"></div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-lg-3 control-label">Logo</label>
                                    <div class="col-lg-5">
                                        <input type="file" class="form-control" name='logo' id="logo" >
                                        <!-- <span class="input-group-btn">
                                            <span class="btn btn-default btn-file">
                                                Browse&hellip; <input type="file" name="">
                                            </span>
                                        </span> -->
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-lg-3 control-label">Email </label>
                                <div class="col-lg-5">
                                    <input type="text" class="form-control" name="emailid" id="emailid"/>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-lg-3 control-label">Website</label>
                                <div class="col-lg-5">
                                    <input type="text" class="form-control" name="siteurl" placeholder="http://" id="siteurl" />
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-lg-3 control-label">Phone number</label>
                                <div class="col-lg-5">
                                    <input type="text" class="form-control" name="phonenumber" id="phonenumber"/>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-lg-3 control-label">Fax number</label>
                                <div class="col-lg-5">
                                    <input type="text" class="form-control" name="faxNumber" id="faxNumber" />
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-lg-3 control-label">Address</label>
                                <div class="col-lg-5">
                                <textarea class="form-control" name="address" id="address"></textarea>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-lg-3 control-label">Latitude</label>
                                <div class="col-lg-5">
                                    <input type="text" class="form-control" name="latitude" id="latitude" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-lg-3 control-label">Longitude</label>
                                <div class="col-lg-5">
                                    <input type="text" class="form-control" name="longitude" id="longitude" />
                                </div>
                            </div>
                        </fieldset>

                        <div class="form-group">
                            <div class="col-lg-9 col-lg-offset-3">
                                <button type="submit" class="btn btn-primary" onclick="companyFormValidation()" >Submit</button>
                            </div>
                        </div>
                    </form>

如果您无法确定名称条目,则可能需要更强大的功能,例如每个参数有三个不同的文本框。

答案 3 :(得分:0)

正确的解决方案是为前缀,名字和名称设置三个单独的文本框。姓。现在,这应该可行。

这不是很简单&#34;简短&#34;,但几乎可以处理所有情况。

string prefix = string.Empty;
string firstName = string.Empty;
string lastName = string.Empty; //no MiddleName, as per your question

var fullName = "Dr. Riwaz N David"; //example

if (string.IsNullOrWhiteSpace(fullName))
    return; //show error
var words = fullName.Trim().Split(' ');

if (words.Length == 1) //only firstname
{
    firstName = words[0];
    return;
}
if (words.Length == 2) //firstname, lastname
{
    firstName = words[0];
    lastName = words[1];
    return;
}

lastName = words[words.Length - 1]; //David
if (Regex.Match(words[0], "^[A-Za-z]{2,3}[.]$").Success) //Mrs. Dr. etc
{
    prefix = words[0]; //Dr.
    firstName = string.Join(" ", words.Skip(1).Take(words.Length - 2)); //Riwad N
}
else //when fullName = "Riwaz N David" no prefix
{
    firstName = string.Join(" ", words.Take(words.Length - 1)); //Riwaw N 
}