如何从字符串右侧读取字符 [英] How to read characters from right side in string

查看:101
本文介绍了如何从字符串右侧读取字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何从字符串右侧读取字符............





请建议



我尝试了什么:



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


  1. 反转字符串
  2. 从左侧获取想要的字符





请参阅 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


  1. Reverse the string
  2. Take the wanted characters from the left



See c# - Best way to reverse a string - Stack Overflow[^].


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆