English 中文(简体)
VB.NET: 将含有“ 姓, 姓” 的字符串转换为“ 姓, 姓” 。
原标题:VB.NET: Convert string containing "Lastname, Firstname" to "Firstname Lastname"

我试图将含有某人名字的字符串 "最后,第一" 转换为"头一" 的字符串。

我现在就是这样做的:

name = name.Trim
name = name.Substring(name.IndexOf(",") + 1, name.Length) & " " & name.Substring(0, name.IndexOf(",") - 1)

当我这样做时,我会有以下错误:

例外的参数外出未处理

索引和长度必须指字符串中的位置

参数名称:长度

谁能解释一下我为什么会犯这个错误 以及我该怎么做?

最佳回答

您正在获取此错误 :

name.Substring(name.IndexOf(",") + 1, name.Length)

name.Length 本应在逗号 之前与字符串 的长度相减。

最好的办法就是分弦

Dim oFullname as string = "Last, First"
Dim oStr() as string = oFullname.split(","c)
oFullname = oStr(1).trim & " " & oStr(0).trim
MsgBox (oFullname)
问题回答

简单,您只需要拆分字符串

Dim originalName As String = "Last,First"
Dim parts = name.Split(","C)
Dim name As String = parts(1) & "  " & parts(0)

如果您正在使用 Unix 命令行 -- -- 类似于Mac - 您可以使用这样的终端 :

假设您有一个包含您最后的 comma- space- first type 名称的文件, 像这样 :

Last1, First1
Last2, First2
Last3, First3

OK, 现在让我们将它保存为 last_ comma_ space_ first. txt 。 您可以在此使用此命令, 我为您的特殊问题而想出 :

sed -E  s/([A-Za-z0-9]+), ([A-Za-z0-9]+)/2 1/g  last_comma_space_first.txt > first_space_last.txt

- & gt; & gt; & gt; 滚动 - & gt; & gt; & gt;

您完成了! 现在, 请检查 < code> first_ space_ last. txt 文件! @ { 您应该得到以下信息 :

First1 Last1
First2 Last2
First3 Last3

告诉你朋友...

这将保持海报格式。

Name = "Doe,John"
Name = Replace(Name.Substring(Name.IndexOf(","), Name.Length - Name.IndexOf(",")) & " " & Name.Substring(0, Name.IndexOf(",")), ",", "")

结果名称 = "John Doe"





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...