从字符串中获取第一个字符 [英] Get first characters from a String
问题描述
我有一个包含 30 个字符的字符串,但我只需要其中的 20 个.我怎样才能获得前 20 个?
I have a string with 30 characters but i only need the 20 of them. How can i get the first 20?
我试过这个子串(ltrim(rtrim(field_value)),20)
I tried this substring(ltrim(rtrim(field_value)),20)
但是没有用.有什么想法吗?
But didn't work. Any ideas?
推荐答案
在My SQL
中,您可以使用LEFT
(或SUBSTRING
)从一个 string
.由于您需要的是第一个字符,我建议使用 LEFT
:
In My SQL
, you could use LEFT
(or SUBSTRING
) to SELECT
certain number of characters from a string
. Since what you need is the first characters, I suggest to use LEFT
:
SELECT LEFT(field_value, 20) FROM MyTableName
原文:
(由于标签原本是C#
,下面的解决方案提供了C#
问题的解决方案)
(As the tag was originally C#
, the following solution below provides solution for C#
problem)
您应该使用 Substring(0, 20)
.第一个参数 (0
) 是起始索引,第二个参数 (20
) 是您想要获取的 string
的长度.
You should use Substring(0, 20)
. The first argument (0
) is the starting index, and the second argument (20
) is the length of the string
you want to take.
例如:
var str = "123456789012345678901234567890";
str = str.Substring(0, 20);
这篇关于从字符串中获取第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!