N \ r \ nXXXX XXXX -XXXX XXXXX 45 108 10 N \ r \ n \ n \ r \ n \ n \ n \ n \ n \ n ***(10)5 2738372 827172672 2 *** X 345 145/00 \ r \ nXxxxxx:\ r \ nXxxxxx \ r \ nXxxxxx \ r \ nXXXXX 21 \ r \ nXXXXXX 2't Xxx
我有来自PDF的这个字符串 我只想要那些提取特定部分并显示它
提前致谢
答案 0 :(得分:1)
如果它在第4行总是相同的长度,那么这应该这样做:
const int TextLength = 26; // length of text you want
// text is the string that contains the data.
// get the 4th line
string line = text.Split(new[]{"\r\n"}, StringSplitOptions.None)[3];
// and extract the text you want
string foo = line.SubString(TextLength);
答案 1 :(得分:0)
这只是Jim提供的解决方案的替代方案。您可以使用Regular Expressions来获得相同的结果。 (当然,如果预期的位数是常数,则这是当然。否则,您需要稍微调整模式以适应)
源字符串是:
N \ r \ nXXXX XXXX -XXXX XXXXX 45 108 10 N \ r \ n \ n \ r \ n \ n \ n \ n \ n \ n ***(10)5 2738372 827172672 2 *** X 345 145/00 \ r \ nXxxxxx:\ r \ nXxxxxx \ r \ nXxxxxx \ r \ nXXXXX 21 \ r \ nXXXXXX 2't Xxx
使用模式:
\(\ d {2} \)\ S \ d \ S \ d {7} \ S \ d {9} \ S \ d
可以通过以下方式解释:
\(\d{2}\) - Where the string starts with ( and contains 2 numeric digits followed by )
\s\d\ - and is followed by a space and a numeric digit
\s\d{7} - and is then followed by a space and 7 numeric digits
\s\d{9} - and is then followed by a space and 9 numeric digits
\s\d - and finally ending with a space and a single numeric digit
您可以使用:
public static string ParseValue(string sourceStr)
{
string pattern = @"\(\d{2}\)\s\d\s\d{7}\s\d{9}\s\d";
return System.Text.RegularExpressions.Regex.Match(sourceStr, pattern).Value;
}
哪会给你以下结果:
(10) 5 2738372 827172672 2