Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

c# - How can I detect text behind a certain word?

my name is Dillion and I am currently working on a project to read values of cryptocurrency basically my program so far goes on google grabs the HTML of THIS link and searches for the text "United States Dollar". I have had it print out the HTML and I have searched for "united states dollar" and in the same string thing this pops up "34,859.30 United States Dollar". Since the value of Bitcoin is always changing I can't just search for "34,859.30 United States Dollar" and print it out because it is always changing. Can anyone help me learn how to search text behind the "United States Dollar" so i can also include the value of bitcoin?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming the HTML is in a string variable called str:

int startingLocation = str.IndexOf("United States Dollar");
string amount = str.Substring(startingLocation - 10).Trim();

This only works if the string behind the text is 10 characters, which in your example it is. IndexOf() returns the index of the first occurrence of the specified string. Trim() removes unnecessary whitespace.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...