Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by marcool1251 · Apr 17, 2018 at 05:58 AM · stringstringsstring.splitstring comparisonstring formatting

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
 }


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image marcool1251 · Apr 17, 2018 at 07:34 AM 0
Share

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!

avatar image Harinezumi marcool1251 · Apr 17, 2018 at 07:41 AM 0
Share

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.

avatar image
0

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\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)")) {
 
             Debug.Log (item.Value);
         }
 
 
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image marcool1251 · Apr 17, 2018 at 07:04 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges