- Home /
Grab part of a string by looking for keyword
Hello, I was wondering if there was a way to extract a specific part of a string.
This is my string:
newmtl body1
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.5000 0.5000 0.5000
Kd 1.000 1.000 1.000
Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000
map_Kd -o 0 -22.843 0 -s 20.26 22.843 1 https://machinecore.farmcp.com/Ziing3Server/Ziing3.aspx?req=image&maharam&fmt=jpg&qlty=80&mat=466194-005&raw=true&dpi=100
I want to grab the link at the end of this string. Like search for the keyword https to identify the link and copy everything that comes after into a new string. This would be my new string:
https://machinecore.farmcp.com/Ziing3Server/Ziing3.aspx?req=image&maharam&fmt=jpg&qlty=80&mat=466194-005&raw=true&dpi=100
The link that I'm trying to extract may change so I need a way to identify it by just http maybe. Any help or guidance is appreciated. Thank you!
Answer by Harinezumi · Apr 17, 2018 at 07:16 AM
Easiest (but not necessarily perfect) way is to use string.IndexOf(string) and string.Substring(int). Something like this:
string FindUrlAtEnd (string input) {
int startIndexOfUrl = input.IndexOf("http"); // I use just "http" in case your url is not using the secure protocol, but if you know it is definitely "https", then use "https://"
if (startIndexOfUrl < 0) { return null; } // input does not contain "http" in it
return input.Substring(startIndexOfUrl); // gets the rest of the string starting at index
}
Thank you! This does what I need it to do. Why do you say it's not the best way to do it? If the string always ends with an http link then there shouldn't be a problem right? The only problem I could see is if there is something that comes after the link. Or maybe if the link provided doesn't start with http. Thanks Again!
You are welcome, I'm glad I could help! :)
That's exactly why I said it might not be perfect, in case something else comes after the url, or it doesn't start with "http". But if the input is always as expected, then it will work.
Answer by yaezah · Apr 17, 2018 at 06:31 AM
You could try using Regular Expressions like this:
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
public class test : MonoBehaviour {
string str;
// Use this for initialization
void Start () {
str = "newmtl body1 Ns 10.0000 Ni 1.5000 d 1.0000 Tr 0.0000 Tf 1.0000 1.0000 1.0000 illum 2 Ka 0.5000 0.5000 0.5000 Kd 1.000 1.000 1.000 Ks 0.0000 0.0000 0.0000 Ke 0.0000 0.0000 0.0000 map_Kd -o 0 -22.843 0 -s 20.26 22.843 1 https://machinecore.farmcp.com/Ziing3Server/Ziing3.aspx?req=image&maharam&fmt=jpg&qlty=80&mat=466194-005&raw=true&dpi=100";
find ();
}
void find(){
foreach (Match item in Regex.Matches (str, @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)")) {
Debug.Log (item.Value);
}
}
}
Thanks for the reply. I get this error when I try this solution:
error CS1579: foreach statement cannot operate on variables of type `System.Text.RegularExpressions.$$anonymous$$atch' because it does not contain a definition for `GetEnumerator' or is inaccessible
What do those characters mean? I see that Regex.$$anonymous$$atches asks for a string and a pattern which is also a string.
Your answer
Follow this Question
Related Questions
how loop through char in a string to find tags 0 Answers
How to extract part of a string variable? 1 Answer
Does Unity's Mono do a reference compare in its string == operator? 2 Answers
Problem splitting up strings 2 Answers
String.Remove not working 1 Answer