将字符串赋值给字符串数组 [英] Assigining string to string array
问题描述
如何在字符串数组中指定字符串值。是否需要拆分方法来执行此操作。如果是这样,为什么我们必须使用Split()方法。
你不能在字符串数组中指定字符串值 - 你可以分配对字符串数组元素的strign vlaue,但是你只能将数组赋给数组,反之亦然。
所以你可以这样做:
string s = Hello there跨度>;
string [] arr = new string [ 5 ];
arr [ 2 ] = s;但你不能这样做:
< pre lang =cs> string s = 你好有跨度>;
string [] arr = s;因为类型不兼容。
您可以使用Split将字符串分解为子字符串数组:
string s = Hello there;
string [] arr = s.Split(' ');
将为您提供两个字符串的数组,包含hello和there。
当分离用户输入字符串时,这非常有用,例如:
string s = Smith; John; 2 Letsbe Avenue,Luton;
string [] arr = s.Split(' ;');会将输入分解为各种地址部分。
您需要拆分函数,因为您需要指定要将字符串转换为数组的基础。示例:您使用逗号分隔移动电话号码,如果要将其转换为数组,则需要使用逗号分隔。
请告诉我们你的确切查询代码是什么。
祝你好运。
How can I assign string value in string array. Is split method required to do this. If so, Why do we have to use Split() Method.
You can''t "assign a string value in a string array" - you can assign a strign vlaue to an element of a string array, but you can only assign arrays to arrays and vice versa.
So you can do this:
string s = "Hello there"; string[] arr = new string[5]; arr[2] = s;But you can''t do this:
string s = "Hello there"; string[] arr = s;because the types are not compatible.
You can use Split to break a string into an array of substrings:
string s = "Hello there"; string[] arr = s.Split(' ');
which will give you an array of two strings, containing "hello" and "there".
This is very useful when separating user input strings for example:
string s = "Smith;John;2 Letsbe Avenue, Luton"; string[] arr = s.Split(';');would break the input into the various address parts.
Hi,
You need split function because you need to specify on what basis you want to convert string into array. example : you have comma separated mobile number and if you want to convert it into array then you need to split with the comma.
Let us know what is your exact query with code.
best luck.
这篇关于将字符串赋值给字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!