有没有从这个字符串中获取值的简单方法? [英] Is there an easy way of getting values from this string?

查看:55
本文介绍了有没有从这个字符串中获取值的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



问题与昨天有关,但我有一个字符串,其中包含数字

昨天987654321我问过如何扭转它并得到一个方法(谢谢!)

到12345678我现在发现它应该显示为21436587 12需要显示为21(这是由于一些时髦的固件编码它现在我的问题是开始工作!)所以我把字符串拆分为1 2 3 4 5 6 7 8使用空格作为分隔符我可以使用foreach循环来逐步执行并将空格作为沿着线的分隔符

Hi All,

Question is related to one yesterday but I have a string which contain numbers
such as 987654321 yesterday I asked how to reverse it and got a method (thank you!)
to 12345678 I have now discovered it should be displayed as 21436587 the 12 need to displayed as 21 (this is due to some funky firmware coding it is now my issue to get working!) so I have split up the string to be 1 2 3 4 5 6 7 8 using a space as a delimeter could I use a foreach loop to step through and a space as a delimeter along the lines of

 StringBuilder Swap = new StringBuilder();  
string store;
string store_Prev;
string combine;
foreach(string value in Returned_Trim.Split(' '))
{
store_Prev = store;
value = store;
combine = store+store_Prev;
Swap.Append(combine);

}  



只是将代码框中的上述代码混合在一起可能有很多语法错误,但你可以看到(希望)我的意思。

Glenn


just hack together the above code in the code box probably lots of syntax errors but you can see (hopefully) what I mean.
Glenn

推荐答案

以下是一个简单解决方案的示例:



Here is an example of a simple solution:

string strInput = "87654321";
string strOutput = "";
long intOutput = 0;
int x = 0;
// Ensure an even number of digits
if (strInput.Length % 2 != 0) {
    Interaction.MsgBox("Input string must be an even number of hexadecimal characters");
    System.Environment.Exit(0);
}
for (x = strInput.Length - 4; x >= 0; x += -2) {
    strOutput += strInput.Substring(x, 2);
}
strOutput = strInput.Substring(strInput.Length - 2, 2) + strOutput;
try {
    // Use Try block in case strInput did not contain Hexadecimal characters
    intOutput = Convert.ToInt64(strOutput, 16);
} catch (Exception ex) {
    Interaction.MsgBox(ex.Message);
    System.Environment.Exit(0);
}
Interaction.MsgBox(strOutput + "=" + intOutput.ToString());







这是上面例子中移动字符的代码根据需要:


.
.
This is the code from the example above that moves the characters around as you require:

for (x = strInput.Length - 4; x >= 0; x += -2) {
    strOutput += strInput.Substring(x, 2);
}
strOutput = strInput.Substring(strInput.Length - 2, 2) + strOutput;


没有必要到拆分,这可以从字符串逐字符完成:

There''s no need to Split, this can be done character-by-character from the string:
StringBuilder Swap = new StringBuilder();
int pos = 0;
foreach (char c in Returned_Trim)
{
  if ((pos & 1) == 1)
  {
    Swap.Insert(pos-1, c);
  }
  else
  {
    Swap.Append(c);
  }
  ++pos;
}



请记住 StringBuilder 可以处理更多类型而不仅仅是<$ c构建时$ c>字符串。


Remember that StringBuilder can deal with more types than just string when building.


如果您的业务规则只是颠倒字符串的每两个字符的顺序,您可以使用这个简单的代码: br $>


If you business rule is simply to reverse the order of every two chars of a string you could use this simple code:

string input = ""123456789;
string output = string.Empty;
for (int i = 0; i < input.Length - 1; i += 2)
     output = string.Format("{0}{1}{2}", output, input[i + 1], input[i]);





如果你的字符串很长我会像这样使用StringBuilder:



If your string is really long I would use StringBuilder like this:

string input = "hdfshfjvsnghvdf4fh12gy5hh2g2h4rtf5htf5bh1xtfh45tfh5f5hf445th46tfh4646t486t3f1htyh84f";
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.Length - 1; i += 2)
     output.AppendFormat("{0}{1}", input[i + 1], input[i]);


这篇关于有没有从这个字符串中获取值的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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