12 Replies - 1243 Views - Last Post: 22 April 2016 - 12:52 PM Rate Topic: -----

#1 Taikiryo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-March 16

Text in file to lowercase

Posted 21 April 2016 - 02:27 PM

Hi guys, I need your help.
I have a program to find all text in file and then replace accordances with another text.
I want to make the text lowercase before replacing. How should I do that?
I tried str.ToLower() but didn't work.
Thank you.
string str = File.ReadAllText("test.txt");
            str = str.Replace("some text", "some other text");
            File.WriteAllText("test.txt", str);



Is This A Good Question/Topic? 0
  • +

Replies To: Text in file to lowercase

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5551
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Text in file to lowercase

Posted 21 April 2016 - 02:34 PM

Well using the str.ToLower() is how you do it...

File.WriteAllText("test.txt", str.ToLower());



This will lowercase the string and pass it to the WriteAllText method to be written to file. This is if you want the entire text file lowercase. If you want to make just the replaced text lowercase then use ToLower() inside your Replace() method.

str = str.Replace("some text", "some other TeXt".ToLower());



If you still can't get it, make sure that your file is actually being re-written back to the file properly and show us your code with the ToLower() in it.

:)
Was This Post Helpful? 0
  • +
  • -

#3 Taikiryo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-March 16

Re: Text in file to lowercase

Posted 21 April 2016 - 02:53 PM

So I found the problem.
I wanna do it with .ass (subtitles format)
It can replace a text, but before replacing I want to make the text lowercase.
When I tried it like you said, the text from file disappeared. So..?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7444
  • View blog
  • Posts: 25,075
  • Joined: 05-May 12

Re: Text in file to lowercase

Posted 21 April 2016 - 02:58 PM

Show us your code otherwise we will just be guessing at what could be going wrong.
Was This Post Helpful? 0
  • +
  • -

#5 Taikiryo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-March 16

Re: Text in file to lowercase

Posted 21 April 2016 - 03:01 PM

string str = File.ReadAllText("test.ass");
            //Before replacing I want to make the text lowercase
            str = str.Replace("some text", "some other text"); //now I can replace some english phrases to my mother tongue easily in lowercase
            File.WriteAllText("test.ass", str); 


Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Text in file to lowercase

Posted 21 April 2016 - 03:41 PM

He means the code you used to try lower casing, not what you have now.
Was This Post Helpful? 0
  • +
  • -

#7 Taikiryo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-March 16

Re: Text in file to lowercase

Posted 21 April 2016 - 11:15 PM

So I tried this:

[code string str = File.ReadAllText("test.ass");
File.WriteAllText("test.ass", str.ToLower());
string str1 = File.ReadAllText("test.ass");
str1 = str1.Replace("some text", "some other text");
File.WriteAllText("test.ass", str1);]

[/code]

and this:

 string str = File.ReadAllText("test.ass");
            str = str.ToLower();
            str = str.Replace("some text", "some other text"); 
            File.WriteAllText("test.ass", str); 



View PostTaikiryo, on 21 April 2016 - 11:13 PM, said:

So I tried this:

      string str = File.ReadAllText("test.ass");
            File.WriteAllText("test.ass", str.ToLower());
            string str1 = File.ReadAllText("test.ass");
            str1 = str1.Replace("some text", "some other text");
            File.WriteAllText("test.ass", str1);]




and this:

 string str = File.ReadAllText("test.ass");
            str = str.ToLower();
            str = str.Replace("some text", "some other text"); 
            File.WriteAllText("test.ass", str); 


Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Text in file to lowercase

Posted 22 April 2016 - 12:14 AM

In what way doesn't it work? Are there errors?
Was This Post Helpful? 0
  • +
  • -

#9 Taikiryo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-March 16

Re: Text in file to lowercase

Posted 22 April 2016 - 12:45 AM

There aren't errors..
Subtitles always disappears.
In .txt it works.
In .ass works only text replacing, but idk why..
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7444
  • View blog
  • Posts: 25,075
  • Joined: 05-May 12

Re: Text in file to lowercase

Posted 22 April 2016 - 04:44 AM

From my brief scan of the SSA/ASS file format, two things stood out:
  • The file contents are case sensitive. You cannot blindly make the entire file contents lowercase.
  • The file format is 8-bit. The documentation is a bit contradictory on exactly which 8-bit format. The actual document says DOS format, but the website says UTF-8. Although for English speakers and most Western locales, this does not make a difference assuming pure ASCII text, there is a difference for other languages and locales. Regardless, you have to ensure that when you write the data back out, you write it out in the same 8-bit encoding that you read the file in.

Was This Post Helpful? 0
  • +
  • -

#11 Taikiryo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-March 16

Re: Text in file to lowercase

Posted 22 April 2016 - 09:17 AM

So am I not able to make it lowercase?
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Text in file to lowercase

Posted 22 April 2016 - 10:07 AM

You can make individual text lowercase, but you can't just make the entire file itself lowercase, because of all the control codes and such in the file.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7444
  • View blog
  • Posts: 25,075
  • Joined: 05-May 12

Re: Text in file to lowercase

Posted 22 April 2016 - 12:52 PM

One way to do this could be (in pseudo code):
foreach(line in the sourceFile)
{
    lowerLine = line.ToLower();
    position = lowerLine.IndexOf(sourceString, beginning);
    while (position > 0)
    {
        line = ReplaceStringAt(position, line, sourceString, targetString);
        lowerLine = line.ToLower();
        position = lowerLine.IndexOf(sourceString, position + targetString.Length);
    }
    outputLines.Append(line);
}
File.WriteAllLines(targetFile, outputLines);


string ReplaceStringAt(position, original, source, target)
{
    left = original.Substring(0, position);
    right = original.Substring(position + source.Length);
    return left + target + right;
}



Of course, you still need to be careful about ensuring that none of the source or target strings match with any of the legal keywords in the SSA/ASS file.

The real correct way is to parse the original file into an object that knows the file format, iterate over the subtitle text as known by the object, perform the replacements on that text, and then have the object save itself into a file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1