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

问题描述

大家好,



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

昨天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

推荐答案

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;


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.


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]);


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

08-15 17:18