我有一个类似AX_1234X_12345_X_CXY
的字符串,我希望在第一个下划线 X
之后删除_
,即从1234X
到{{ 1}}。所以最终输出将是1234
。怎么做??如果我使用AX_1234_12345_X_CXY
,它将替换我不想要的所有 .Replace("X", "")
答案 0 :(得分:4)
您可以从第一次出现的library(purrr)
j <- coords %>%
map2(segments, ~{
list(vendor = 0,
startTime = min(.y),
endTime = max(.y),
segments = cbind(.x[[1]][[1]], .y)
)
}) %>%
jsonlite::toJSON(auto_unbox = T, digits = NA)
j
#> [{"vendor":0,"startTime":4380,"endTime":4445,"segments":[[19.9388183333333,50.0654816666667,4380],[19.9388183333333,50.0654816666667,4388.125],[19.93881,50.0655566666667,4396.25],[19.938845,50.0656116666667,4404.375],[19.9388483333333,50.065615,4412.5],[19.9388516666667,50.0656083333333,4420.625],[19.9388516666667,50.0656066666667,4428.75],[19.9388516666667,50.0656066666667,4436.875],[19.9388516666667,50.0656066666667,4445]]},{"vendor":0,"startTime":4448,"endTime":4453,"segments":[[19.9194666666667,50.0720283333333,4448],[19.9194666666667,50.0720283333333,4449.66666666667],[19.9194666666667,50.0720283333333,4451.33333333333],[19.9194666666667,50.0720283333333,4453]]},{"vendor":0,"startTime":4696,"endTime":4880,"segments":[[19.9355333333333,50.06572,4696],[19.93554,50.06568,4716.44444444444],[19.93554,50.06568,4736.88888888889],[19.9355116666667,50.0657416666667,4757.33333333333],[19.9353733333333,50.065765,4777.77777777778],[19.9340316666667,50.06723,4798.22222222222],[19.9339433333333,50.0673366666667,4818.66666666667],[19.9339266666667,50.0673833333333,4839.11111111111],[19.9339266666667,50.0673816666667,4859.55555555556],[19.9339266666667,50.0673816666667,4880]]}]
开始迭代字符串。
您可以使用IndexOf()找到第一次出现的'_'
当循环到达'X'时,它不会将它附加到“固定字符串”。
'_'
答案 1 :(得分:2)
为什么不好老IndexOf
和Substring
?
string s = "AX_1234X_12345_X_CXY";
int pUnder = s.IndexOf('_');
if (pUnder >= 0) { // we have underscope...
int pX = s.IndexOf('X', pUnder + 1); // we should search for X after the underscope
if (pX >= 0) // ...as well as X after the underscope
s = s.Substring(0, pX) + s.Substring(pX + 1);
}
Console.Write(s);
结果:
AX_1234_12345_X_CXY
答案 2 :(得分:1)
一种方法是String.Remove,因为你可以准确地告诉从哪里删除。如果违规的“X”始终在同一个地方,您可以使用:
string newString = old.Remove(7,1);
这将从位置7开始删除1个字符(从零开始计算字符串的开头)。
如果不是总是在同一个角色位置,您可以尝试:
int xPos = old.IndexOf("X");
string newString = old.Remove(xPos,1);
编辑:
基于OP评论,我们定位的“X”只是在第一个下划线字符后,所以让我们从第一个下划线开始索引:
int iPosUnderscore = old.IndexOf("_");
string newString = old.Remove(iPosUnderscore + 1 ,1); // start after the underscore
答案 3 :(得分:1)
string original = @"AX_1234X_12345_X_CXY";
original = @"AX_1234_12345_X_CXY";
答案 4 :(得分:1)
尝试查看string.IndexOf
或string.IndexOfAny
string s = "AX_1234X_12345_X_CXY";
string ns = HappyChap(s);
public string HappyChap(string value)
{
int start = value.IndexOf("X_");
int next = start;
next = value.IndexOf("X_", start + 1);
if (next > 0)
{
value = value.Remove(next, 1);
}
return value;
}
答案 5 :(得分:0)
当且仅当这始终是格式时,它应该是组合原始文本的子串而不包括该位置的x的简单问题。但是,op并未声明情况总是如此。因此,如果这始终是格式,并且始终删除相同的字符位置,那么您可以只是
string s = "AX_1234X_12345_X_CXY";
string newstring = s.Substring(0, 7) + s.Substring(8);
好的,基于第二组数字的长度可变,您可以执行以下操作:
int startpos = s.IndexOf('_', 4);
string newstring = s.Substring(0, startpos - 1) + s.Substring(startpos);
使用此代码,以下测试结果为:
"AX_1234X_12345_X_CXY" became "AX_1234_12345_X_CXY"
"AX_123X_12345_X_CXY" became "AX_123_12345_X_CXY"
"AX_234X_12345_X_CXY" became "AX_234_12345_X_CXY"
"AX_1X_12345_X_CXY" became "AX_1_12345_X_CXY"
答案 6 :(得分:0)
这样的事情可行。我确信这是一个更优雅的解决方案。
string input1 = "AX_1234X_12345_X_CXY";
string pattern1 = "^[A-Z]{1,2}_[0-9]{1,4}(X)";
string newInput = string.Empty;
Match match = Regex.Match(input1, pattern1);
if(match.Success){
newInput = input1.Remove(match.Groups[1].Index, 1);
}
Console.WriteLine(newInput);