-6

Basically what I'm trying to do is find the first string that starts with "/Game/Mods" but the problem is how do i tell the program where to end the string? here's an example what a string can look like: string example

As you can see the string starts with "/Game/Mods", i want it to end after the word "TamingSedative", the problem is that the ending word (TamingSedative)is different for every file it has to check, for example: example 2

There you can see that the ending word is now "WeapObsidianSword" (instead of TamingSedative) so basically the string has to end when it comes across the "NUL" but how do i specify that in c# code?

ataravati
  • 8,891
  • 9
  • 57
  • 89
  • And to be more specific, I'm trying to find the string in a text file that i selected. – Niet Machine Aug 26 '17 at 14:00
  • 3
    Post your code. What have you done so far? – ataravati Aug 26 '17 at 14:02
  • I haven't done much other than usin OpenFileDialog() to select the text file to check for the string, I'm completely clueless. – Niet Machine Aug 26 '17 at 14:13
  • Do you want to get "TamingSedative" or whatever string as output ? – Youssef13 Aug 26 '17 at 14:22
  • No, what i wanna get as output is the string starting from /Game/Mods utill it reaches then end of the word "TamingSedative" in this case, however, as i showed above, the ending word (TamingSedative) is not static and is different for every file, so basically when i select my file it has to find the first string in the text file that starts with "/Game/Mods" and it should end at the "NUL" – Niet Machine Aug 26 '17 at 14:29
  • I'm pretty sure the "NUL" in this case is just seen a whitespace by the way. – Niet Machine Aug 26 '17 at 15:03
  • Well, you need to at least find a pattern in those strings. Are you looking for all the strings that have this pattern: "/Game/Mods/*/*"? If yes, you can easily do that with regular expressions. – ataravati Aug 26 '17 at 15:09
  • Some of the strings have more /'s so i don't think a pattern can be formed that way, the string just has to start at /Game/Mods and end when it reaches the whitespace, one valid string example would then be "/Game/Mods/TamingSedative/PrimalItemConsumable_TamingSedative" – Niet Machine Aug 26 '17 at 15:16
  • @NietMachine, in that case, check my answer. – ataravati Aug 27 '17 at 00:20

3 Answers3

0

This a simple example using Regex.

Dim yourString As String = "/Game/Mods/TamingSedative/PrimalItemConsumable_TamingSedative"
Dim M As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(yourString, "/Game/Mods/(.+?)/")
MessageBox.Show(M.Groups(0).Value) 'This should show /Game/Mods/TamingSedative/
MessageBox.Show(M.Groups(1).Value) 'This should show TamingSedative
Youssef13
  • 3,836
  • 3
  • 24
  • 41
0

Since you need only the first occurance, this is the simplest solution I could think of:

(In case you cannot see the image, click on it to open in new tab)

enter image description here

EDIT:

In case the existence of a path like this is not guaranteed in the string, you can do an additional check before proceeding to use Substring, like this:

int exists = fullString.IndexOf("/Game/Mods");
if (exists == -1) return null;

Note: I have included "ENDED" in order to see in case any NULL chars have been included (white spaces)

Hubbs
  • 163
  • 11
  • This seems like a great solution but why did you add the \0's? – Niet Machine Aug 26 '17 at 15:35
  • @NietMachine \0's simulate a NULL character (terminating zero). The NUL character you see in Notepad++ is actually a \0 (terminating zero). – Hubbs Aug 26 '17 at 15:36
  • Oh wow, i didn't know that! thanks for telling me, i will try out this solution right now and see if i can manage to get it to work. – Niet Machine Aug 26 '17 at 15:40
  • @NietMachine I have EDIT'd my post including the same solution but if you want to pass a whole string which includes multiple paths inside of it, it's going to use a List to store the found substrings and return them. Same approach, a bit different tho – Hubbs Aug 26 '17 at 15:41
  • The whole string in this case would be the contents of a .txt file, and only the first occurance would need to be returned. – Niet Machine Aug 26 '17 at 15:54
  • @NietMachine So you need only the first occurance of a "Path" starting with /Game/Mods/ ? Then you can use the first picture in my post which returns only the first occurance. – Hubbs Aug 26 '17 at 16:04
  • Yes, exactly. I've come across one problem though, when i try to read the file contents using ReadAllText or ReadToEnd they both return "??*?????`" is this because the file contains foreign characters? and is it possible to fix this? – Niet Machine Aug 26 '17 at 16:12
  • @NietMachine Yes, this is because of the Encoding of the file most likely. In case the encoding is unknown you can use the the `StreamReader` type which supports detecting these marks to determine the encoding, the only thing you need to do is define it as such: `var strReader = new System.IO.StreamReader("path", true);` The `true` parameter here specifies that you want to detect the encoding from bytes . After declaring it like this you can use it to read the file. – Hubbs Aug 26 '17 at 16:20
  • @NietMachine One thing to pay attention to is that StreamReader's ability to detect the encoding works ONLY when the encoding is a Unicode-based encoding. – Hubbs Aug 26 '17 at 16:22
  • I'm pretty sure the encoding is some unknown format, isn't it just possible to read the insides of a file no matter what encoding is used? i mean if you skip the foreign characters it's pretty readable.. – Niet Machine Aug 26 '17 at 17:34
  • @NietMachine When I meant unknown format, I meant unknown for you. There's no such thing as unknown format in general. It's just that you understand what the format is and use it to read the file. Search through Stackoverflow, there are a lot of threads about this. I'm pretty sure you can handle this problem. Regarding the path, you can use the code I provided you with. – Hubbs Aug 26 '17 at 19:04
  • The format is uasset, which is used in unreal engine (UE4), it's partially readable in a text editor though, and i need to find the string inside of that file – Niet Machine Aug 26 '17 at 19:09
  • @NietMachine Since you said it's "partially readable", when you use StreamReader to read the file, if the string inside that you want to find is actually readable, then you can use the method in my answer straight away, without having to worry about the encoding.. – Hubbs Aug 26 '17 at 19:13
  • That is exactly what i'm trying to do but when i Console.Log the result all i get is: ??*?????` and then sometimes 1 or 2 words but that's about it.. – Niet Machine Aug 26 '17 at 19:39
  • After fiddling around i now get the error "Length cannot be less than zero" on the last line of your first example – Niet Machine Aug 26 '17 at 19:48
  • @NietMachine Read the EDIT that I have done to the post several hours ago.. It is possible that the path you're looking doesn't exist in the string, include those 2 lines in the EDIT, make them the first 2 lines in the function and see. – Hubbs Aug 26 '17 at 19:58
  • The thing is though, the existence of "/Game/Mods" is guaranteed in the string, i've manually checked myself.. – Niet Machine Aug 26 '17 at 20:09
  • @NietMachine In the file yeah, but when reading the string, the actual string that you receive when reading from the file, does it contain the path? Or it's showing weird stuff because of the encoding? – Hubbs Aug 26 '17 at 20:17
  • I tried putting the file contents into a textbox and this was the result...https://i.gyazo.com/5b72313ce66d5f56b9811f1c0d9b6f8e.png https://i.gyazo.com/f07dc3c874b5b7884ab651696699bd03.png – Niet Machine Aug 26 '17 at 20:39
0

From your comments: "the string just has to start at /Game/Mods and end when it reaches the whitespace".

In that case, you can easily get the matches using Linq, like this (assuming filePath is a string that has the path to your file):

var text = File.ReadAllText(filePath);

var matches = text.Split(null).Where(s => s.StartsWith("/Game/Mods"));

And, if you only need the first occurrence, it would be:

var firstMatch = matches.Any() ? matches.First() : null;

Check this post.

ataravati
  • 8,891
  • 9
  • 57
  • 89