如何从字符串右侧读取字符 [英] How to read characters from right side in string
本文介绍了如何从字符串右侧读取字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从字符串右侧读取字符............
请建议
我尝试了什么:
string =samplestring;
string.Substring(string.Length - 4,4);
它给出结果,但是从左侧...我想从右侧读取4个字符。
解决方案
试试这个
string input = samplestring;
var str = input.Substring(input.Length - 4 , 4 跨度>);
string output = ;
for ( int i = str.Length -1; i > = 0 ; i--)
output + = str [i] .ToString();
// 输出将为gnir
- 反转字符串
- 从左侧获取想要的字符
请参阅 c# - 反转字符串的最佳方法 - Stack Overflow [ ^ ]。
您的示例代码甚至不会编译,更不用说运行了,因此很难看出您是如何设法获得任何结果的。正确的代码如下:
string foo = samplestring;
string bar = foo.Substring(foo.Length - 4 , 4 跨度>);
Console.WriteLine( 子字符串:{0},bar);
Hi,
how to read characters from right side in string............
Please suggest
What I have tried:
string="samplestring";
string.Substring(string.Length - 4, 4);
it gives the result, but from left side... i want to read 4 characters from right side.
解决方案
try this
string input = "samplestring"; var str = input.Substring(input.Length - 4, 4); string output = ""; for (int i = str.Length -1; i >= 0; i--) output += str[i].ToString(); // output will be gnir
- Reverse the string
- Take the wanted characters from the left
Your sample code will not even compile, let alone run, so it is difficult to see how you manage to get any result. The correct code would be something like:
string foo ="samplestring"; string bar = foo.Substring(foo.Length - 4, 4); Console.WriteLine("Substring: {0}", bar);
这篇关于如何从字符串右侧读取字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文