你如何使用string.substring? [英] How do you use the string.substring?
问题描述
我有一项任务,我需要找到一个解决方案。任务是:
写一个ac#program,它接受一个物品代码并显示国家原产地。
物品代码的例子是NIUK0023。
NI-北爱尔兰
英国 - 英国
任何其他角色暗示欧洲国家
我需要知道如何使用string.substring方法来执行此操作。
感谢您的帮助。
我尝试过:
int itemcode;
Console.WriteLine(输入商品代码:(例如NIUK0023);
字符串值= Console.ReadLine();
int startIndex = 5;
int length = 2;
String substring = value.Substring(startIndex,长度);
Console.WriteLine(substring);
原产国是(索引0,长度2),绑定国家i s(索引2,长度2)。想起来并不是很难。有关详细信息,请参阅字符串类(系统) [< a href =https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]。
假设你的国家代码是两个字符 - 这里有一个列表:国家代码清单 - ISO ALPHA-2,ISO ALPHA-3和数字国家代码 - 国家在线项目 [ ^ ],但它将NI列为尼加拉瓜 - 您需要做两件事。
1)在循环中处理您的用户输入提取每两个数字对。这并不困难,你只需使用substring来提取前两个,然后从字符串中删除它们:
while (!string.IsNullOrWhiteSpace(strInput))
{
string cc = strInput.Substring( 0 , 2 );
strInput = strInput.Length > 2 ? strInput.Substring( 2 ): ;
...
}
2)查找国家/地区代码并将其转换为人类可读的字符串。我不能告诉你如何做到这一点:我知道我是怎么做的,但是它使用的代码你可能还没有遇到过:字典和文件。因此,您的最佳方式可能是使用开关
块:
开关(cc)
{
案例 UK:strHumanReadable = 英国; break ;
case ... $ b $使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
名称空间ConsoleApp8
{
class program
{
static void Main(string [] args)
{
string countryOfOrgion;
string countrycode;
string ItemCode;
ItemCode =NIUK0023; //为了方便,硬编码输入
countryOfOrgion = ItemCode.Substring(0,2);
if(countryOfOrgion.Equals(NI))
{
countrycode =Northern Ireland;
}
else if(count ryOfOrgion.Equals(UK))
{
countrycode =United Kingdom;
}
否则
{
countrycode =欧洲国家;
}
Console.WriteLine(countryOfOrgion +'\\ \\ t'+国家代码);
Console.ReadLine();
}
}
}
I have a task i need to find a solution to. the task is :
Write a c# program which accepts an item code and displays the country origin.
The example of a item code is NIUK0023.
NI-Northern Ireland
UK-United Kingdom
any other characters imply a European Country
I need to know how i can use a string.substring method to do this.
Thanks for the help.
What I have tried:
int itemcode;
Console.WriteLine("Enter an Item Code: (E.g. NIUK0023");
String value = Console.ReadLine();
int startIndex = 5;
int length = 2;
String substring = value.Substring(startIndex, length);
Console.WriteLine(substring);
Country of origin is (index 0, length 2), bound country is (index 2, length 2). Not really too difficult to figure out. For further information see String Class (System)[^].
Assuming your country codes are two characters - and there is a list of them here: Country Codes List - ISO ALPHA-2, ISO ALPHA-3 and Numerical Country Codes - Nations Online Project[^] , but it lists NI as Nicaragua - you will need to do two things.
1) Process your user input in a loop to extract each two digit pairs. This isn't difficult, you just use substring to extract the first two, then remove them from the string:
while (!string.IsNullOrWhiteSpace(strInput)) { string cc = strInput.Substring(0, 2); strInput = strInput.Length > 2 ? strInput.Substring(2) : ""; ... }
2) Look up the country code and translate it to a human readable string. I can't tell you how to do that: I know how I'd do it, but that uses code you probably haven't met yet: Dictionaries and files. So your "best" way is probably to use aswitch
block:
switch(cc) { case "UK": strHumanReadable = "United Kingdom"; break; case "... }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
string countryOfOrgion;
string countrycode;
string ItemCode;
ItemCode = "NIUK0023";//for ease, hard code input
countryOfOrgion = ItemCode.Substring(0, 2);
if(countryOfOrgion.Equals("NI"))
{
countrycode = "Northern Ireland";
}
else if (countryOfOrgion.Equals("UK"))
{
countrycode = "United Kingdom";
}
else
{
countrycode = "European Country";
}
Console.WriteLine(countryOfOrgion + '\t' + countrycode);
Console.ReadLine();
}
}
}
这篇关于你如何使用string.substring?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!